javascript 正则表达式允许法文和英文文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19652188/
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
Regular Expression to allow french text as well as english text?
提问by Suvankar Bhattacharya
I want to use a regular expression which will allow
我想使用一个正则表达式,它将允许
- English text which does not have a special character.
- French Text which does not have a special character.
- 没有特殊字符的英文文本。
- 没有特殊字符的法语文本。
It will always disallow special characters like @, #, % etc... in both the language.
它将始终禁止在这两种语言中使用特殊字符,如 @、#、% 等。
I have tried with the below code:
我已尝试使用以下代码:
if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}
It works fine with english text, but the problem is when I provide a french text like éléphant, it considers the french characters as special character, and deletes the french characters. so éléphant becomes lphant.
它适用于英文文本,但问题是当我提供像 éléphant 这样的法语文本时,它会将法语字符视为特殊字符,并删除法语字符。所以 éléphant 变成了 lphant。
Is there any way to allow the french characters inside the regular expression?
有没有办法允许正则表达式中的法语字符?
Thanks a lot in advance.
非常感谢。
回答by mortb
Quick solution:
快速解决方案:
/[^a-zA-Z0-9 àa?èéê?????ù?ü??à??èéê?????ù?ü??]/
Reference: List of french characters
参考: 法语字符列表
Hope this helps
希望这可以帮助
回答by Sam G
Most simplified solution:
最简化的解决方案:
/[^a-zA-Zà-?]/
(or)
(或者)
/[\wà-?]/ // Note: This will allow "_" also
Any of the above regular expression will work in your case.
上述任何正则表达式都适用于您的情况。
回答by Andrés Oviedo
I would suggest normalizingstring before replacing chars.
我建议在替换字符之前标准化字符串。
This example is a JAVA normalization, but maybe this examplecould help you with javascript
这个例子是一个 JAVA 规范化,但也许这个例子可以帮助你使用 javascript
String string = "éléphante";
string = Normalizer.normalize(string, Normalizer.Form.NFD);
string = string.replaceAll("[^\p{ASCII}]", "");
System.out.println(string.replaceAll("[^a-zA-Z0-9 ]", ""));