正则表达式匹配名称(Regular expression to match a name)

编程入门 行业动态 更新时间:2024-10-28 14:32:43
正则表达式匹配名称(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 Josh

Could 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À-ÖØ-öø-ſ]+$

更多推荐

本文发布于:2023-08-07 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   正则表达式   Regular   expression   match

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!