I'm using a simple Javascript regular expression to validate people's names. However, I do not want to allow a user to enter accented characters like ã
, ä
, õ
, ö
, etc. I am aware that these can actually be valid characters in a name, but for my exercise, I need to be able to filter them out.
我正在使用一个简单的Javascript正则表达式来验证人们的名字。但是,我不想让用户输入重音字符,如ã,ä,õ,ö等。我知道这些字符实际上可以是名字中的有效字符,但对于我的练习,我需要能够过滤掉它们。
My current regex:
我现在的正则表达式:
^[a-zA-Z]+('|-|.|)[a-zA-Z\s]+$
This matches accented characters as well. How do I modify this regex so that it doesn't match names with accents (or any Unicode character, for that matter)?
这也匹配重音字符。如何修改此正则表达式,使其与带重音的名称(或任何Unicode字符)不匹配?
2 个解决方案
#1
2
Remove .
, because .
match any character. That could cause matching accented character.
删除。,因为。匹配任何角色。这可能会导致匹配的重音字符。
^[a-zA-Z]+('|-)[a-zA-Z\s]+$
or escape .
if you mean literal dot.
或逃避。如果你的意思是字面点。
^[a-zA-Z]+('|-|\.)[a-zA-Z\s]+$
#2
1
To match a single character it's better to use a class [...]
, not a group with alternatives (...|...)
:
要匹配单个字符,最好使用类[...],而不是具有替代项的组(... | ...):
^[a-zA-Z]+['.-][a-zA-Z\s]+$
Note that this is not 100% accurate, since it validates things like foo.bar
etc.
请注意,这不是100%准确,因为它验证了诸如foo.bar之类的东西。
#1
2
Remove .
, because .
match any character. That could cause matching accented character.
删除。,因为。匹配任何角色。这可能会导致匹配的重音字符。
^[a-zA-Z]+('|-)[a-zA-Z\s]+$
or escape .
if you mean literal dot.
或逃避。如果你的意思是字面点。
^[a-zA-Z]+('|-|\.)[a-zA-Z\s]+$
#2
1
To match a single character it's better to use a class [...]
, not a group with alternatives (...|...)
:
要匹配单个字符,最好使用类[...],而不是具有替代项的组(... | ...):
^[a-zA-Z]+['.-][a-zA-Z\s]+$
Note that this is not 100% accurate, since it validates things like foo.bar
etc.
请注意,这不是100%准确,因为它验证了诸如foo.bar之类的东西。