javascript 忽略正则表达式中的重音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13193071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ignore accent in regex
提问by Christoph
Possible Duplicate:
How to ignore acute accent in a javascript regex match?
I have some javascript as :
我有一些 javascript 为:
var myString = 'préposition_preposition';
var regex = new RegExp("epo", "ig");
alert(myString.match(regex));
is it possible to match "épo" and "epo", if I set in regex only epo (or only épo)?
如果我仅在正则表达式中设置 epo(或仅 épo),是否可以匹配“épo”和“epo”?
回答by Christoph
I had the same problem recently. Regex operates with ascii, therefor special characters like é
or ?
are not recognized. You need to explicitely include those into your regex.
我最近遇到了同样的问题。正则表达式使用 ascii 进行操作,因此无法识别é
或?
不识别特殊字符。您需要明确地将它们包含在您的正则表达式中。
Use this:
用这个:
var regex = /[ée]po/gi;
Hint: Don't use new Regex()
it's rather slow, but declare the regex directly instead. This also solves some quoting/escaping issues.
提示:不要使用new Regex()
它很慢,而是直接声明正则表达式。这也解决了一些引用/转义问题。
回答by clentfort
No you can not achieve this behavior. RegEx match exactly the string you provided. How should the computer know when épo
or epo
is what you are looking for!
不,您无法实现此行为。RegEx 与您提供的字符串完全匹配。计算机如何知道您要查找的内容épo
或epo
内容!
But you can specify a class of chractersthat can be matched new RegExp("[eé]po", "ig");
回答by Minko Gechev
Try this:
试试这个:
var str = 'préposition_preposition';
str.match(/(e|é)po/gi);