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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:02:14  来源:igfitidea点击:

ignore accent in regex

javascripthtmlregex

提问by Christoph

Possible Duplicate:
How to ignore acute accent in a javascript regex match?

可能的重复:
如何在 javascript 正则表达式匹配中忽略重音?

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 époor epois what you are looking for!

不,您无法实现此行为。RegEx 与您提供的字符串完全匹配。计算机如何知道您要查找的内容époepo内容!

But you can specify a class of chractersthat can be matched new RegExp("[eé]po", "ig");

但是你可以指定一可以匹配的字符new RegExp("[eé]po", "ig");

回答by Minko Gechev

Try this:

试试这个:

var str = 'préposition_preposition';
str.match(/(e|é)po/gi);