[英]Regular expression to match a name
I am trying to write a regular expression in Javascript to match a name field, where the only allowed values are letters, apostrophes and hyphens. 我试图在Javascript中编写一个正则表达式来匹配名称字段,其中唯一允许的值是字母,撇号和连字符。 For example, the following names should be matched: 例如,应匹配以下名称:
jhon's
avat-ar
Josh
Could someone please help me construct such a regex? 有人可以帮我构建一个这样的正则表达式吗?
Yes. 是。
^[a-zA-Z'-]+$
Here, 这里,
^
means start of the string, and $
means end of the string. ^
表示字符串的开头, $
表示字符串的结尾。 […]
is a character class which anything inside it will be matched. […]
是一个角色类,里面的任何东西都会被匹配。 x+
means the pattern before it can be repeated once or more. x+
表示在重复一次或多次之前的模式。 Inside the character class, 在角色类里面,
az
and AZ
are the lower and upper case alphabets, az
和AZ
是小写和大写字母, '
is the apostrophe, and '
是撇号,而且 -
is the hyphen. -
是连字符。 The hyphen must appear at the beginning or the end to avoid confusion with the range separator as in az
. 连字符必须出现在开头或结尾,以避免与az
的范围分隔符混淆。 Note that this class won't match international characters eg ä. 请注意,此类不会与国际字符匹配,例如ä。 You have to include them separately eg 你必须单独包括它们,例如
^[-'a-zA-ZÀ-ÖØ-öø-ſ]+$
A compact version for the UTF-8 world that will match international letters and numbers. 适用于UTF-8世界的紧凑版本,可与国际字母和数字相匹配。
/^[\p{L}\p{N}*-]+$/u
Explanation: 说明:
Note, that if the hyphen is the last character in the class definition it does not need to be escaped . 请注意,如果连字符是类定义中的最后一个字符,则不需要对其进行转义 。 If the dash appears elsewhere in the class definition it needs to be escaped , as it will be seen as a range character rather then a hyphen. 如果破折号出现在类定义的其他位置,则需要对其进行转义 ,因为它将被视为范围字符而不是连字符。
更紧凑的版本是[\\w'-]+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.