正则表达式删除非字母字符但保留重音字母 [英] Regex to remove non-letter characters but keep accented letters

查看:123
本文介绍了正则表达式删除非字母字符但保留重音字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有西班牙语和其他语言的字符串,可能包含通用特殊字符,如(),*等。我需要删除。但问题是它也可能包含特殊的语言字符,如ñ,á,ó,í等,它们需要保留。所以我试图用以下方式用regexp来做:

I have strings in Spanish and other languages that may contain generic special characters like (),*, etc. That I need to remove. But the problem is that it also may contain special language characters like ñ, á, ó, í etc and they need to remain. So I am trying to do it with regexp the following way:

var desired = stringToReplace.replace(/[^\w\s]/gi, '');

不幸的是,它删除了所有特殊字符,包括相关语言。不知道如何避免这种情况。也许有人可以建议?

Unfortunately it is removing all special characters including the language related. Not sure how to avoid that. Maybe someone could suggest?

推荐答案

我建议使用Steven Levithan的优秀 XRegExp 库及其 Unicode插件

I would suggest using Steven Levithan's excellent XRegExp library and its Unicode plug-in.

这是一个从字符串中删除非拉丁字符的例子: http://jsfiddle.net/b3awZ/1/

Here's an example that strips non-Latin word characters from a string: http://jsfiddle.net/b3awZ/1/

var regex = XRegExp("[^\\s\\p{Latin}]+", "g");
var str = "¿Me puedes decir la contraseña de la Wi-Fi?"
var replaced = XRegExp.replace(str, regex, "");

请参阅Steven Levithan本人的回答:

See also this answer by Steven Levithan himself:

正则表达式西班牙语和阿拉伯语单词

这篇关于正则表达式删除非字母字符但保留重音字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆