正则表达式匹配名称(Regular expression to match a name)
我试图在Javascript中编写一个正则表达式来匹配名称字段,其中唯一允许的值是字母,撇号和连字符。 例如,应匹配以下名称:
jhon's avat-ar Josh有人可以帮我构建这样的正则表达式吗?
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. For example, the following names should be matched:
jhon's avat-ar JoshCould someone please help me construct such a regex?
最满意答案
是。
^[a-zA-Z'-]+$这里,
^表示字符串的开头, $表示字符串的结尾。 […]是一个字符类,其中的任何内容都将匹配。 x+表示在重复一次或多次之前的模式。在角色类里面,
az和AZ是小写和小写字母, '是撇号,而且 -是连字符。 连字符必须出现在开头或结尾,以避免与az的范围分隔符混淆。请注意,此课程不会与国际字符匹配,例如ä。 你必须单独包括它们,例如
^[-'a-zA-ZÀ-ÖØ-öø-ſ]+$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.Inside the character class,
a-z and A-Z are the lower and upper case alphabets, ' 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 a-z.Note that this class won't match international characters e.g. ä. You have to include them separately e.g.
^[-'a-zA-ZÀ-ÖØ-öø-ſ]+$更多推荐
发布评论