问题描述
我需要验证文本输入,以便用户可以插入可能包含德语变音、法语重音和任何其他有效欧洲字符(例如小写 ø)的字符/文本.
I need to validate a text input so a user can insert characters/text that may include German umlauts, French accents and any other valid European characters, for example the minuscule ø.
我正在使用 AngularJS,所以我将我的验证规则应用于 ng-pattern
属性,如下所示:
I am using AngularJS so I am applying my validation rule to the ng-pattern
attribute like so:
ng-pattern="/^[A-Za-z0-9 \-_.]*$/"
我希望这能覆盖像 äöüß 这样的字符,但在测试时却没有.很抱歉问这样一个蹩脚的问题,但我在 RegEx 方面真的很糟糕!肯定有比手动列出字母更好的方法 ng-pattern="/^[A-Za-z0-9äöüÄÖÜ \-_.]*$/"
I was hoping this would cover characters like äöüß but when testing it doesn't. Sorry to ask such a lame question but I am really bad at RegEx! There must be a better way than manually listing the letters like so ng-pattern="/^[A-Za-z0-9äöüÄÖÜ \-_.]*$/"
推荐答案
Javascript 正则表达式不支持 unicode 属性,包含非拉丁字母的唯一方法是在表达式中明确列出它们:
Javascript regexes don't support unicode properties, the only way to include non-latin letters is to list them explicitly in your expression:
[A-Za-z0-9 \-_.äöüß etc]
或使用 unicode 范围,例如
or use unicode ranges, e.g
[A-Za-z0-9 \-_.\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF]
(请参阅 http://www.utf8-chartable.de/ 以供参考).
(see http://www.utf8-chartable.de/ for reference).
这篇关于修改正则表达式以允许使用德语变音、法语口音和其他有效的欧洲字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!