最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression to match a name - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited May 1, 2012 at 3:27 Perception 80.6k19 gold badges188 silver badges196 bronze badges asked Apr 27, 2010 at 13:54 zoom_pat277zoom_pat277 1,2245 gold badges15 silver badges31 bronze badges 7
  • 5 First off, define "letters". Because there's an awful lot of them – Tomalak Commented Apr 27, 2010 at 13:58
  • letters would be 'a to z' both upper and lower cases, since they are required for an input of first name and last name fields – zoom_pat277 Commented Apr 27, 2010 at 14:00
  • possible duplicate of stackoverflow.com/questions/421046/… – Joachim Sauer Commented Apr 27, 2010 at 14:03
  • well! you have a valid point over here, but my requirement docs just says letters, apost, and hyphons... I do not even know what are those 'u' in 'Jürgen Müller' are called... but I would be curious to know, as to what are those charachters called and how can we come up with the regular expression to match them... – zoom_pat277 Commented Apr 27, 2010 at 14:07
  • @zoom: Time to go back and clarify your requirements, I guess. ;) It baffles me every time when people find out that there are more letters in the world than in US-ASCII. – Tomalak Commented Apr 27, 2010 at 14:10
 |  Show 2 more comments

3 Answers 3

Reset to default 13

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

A compact version for the UTF-8 world that will match international letters and numbers.

/^[\p{L}\p{N}*-]+$/u

Explanation:

  • [] => character class definition
  • p{L} => matches any kind of letter character from any language
  • p{N} => matches any kind of numeric character
  • *- => matches asterisk and hyphen
  • + => Quantifier — Matches between one to unlimited times (greedy)
  • /u => Unicode modifier. Pattern strings are treated as UTF-16. Also causes escape sequences to match unicode characters.

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.

More compact version is [\w'-]+

发布评论

评论列表(0)

  1. 暂无评论