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

javascript - Include the hyphen into this regular expression, how? - Stack Overflow

programmeradmin10浏览0评论

I have some JavaScript code using a regex, to validate a name field in a form:

  var alphaExp = /^[a-zA-ZåäöÅÄÖ\s]+$/;

I want the regex to allow names that include a hyphen, such as Anna-nicole.

Ideally, the regex should reject names that start or end with a hyphen.

How can I modify the regex to make this work? I get an error if I try to add a hyphen to the character class.

I have some JavaScript code using a regex, to validate a name field in a form:

  var alphaExp = /^[a-zA-ZåäöÅÄÖ\s]+$/;

I want the regex to allow names that include a hyphen, such as Anna-nicole.

Ideally, the regex should reject names that start or end with a hyphen.

How can I modify the regex to make this work? I get an error if I try to add a hyphen to the character class.

Share Improve this question edited May 3, 2024 at 20:17 Karl Knechtel 61.4k14 gold badges126 silver badges184 bronze badges asked Oct 24, 2010 at 18:53 user188962user188962 3
  • You currently allow spaces at both ends - do you want them limited too? And do you want to outlaw 'Nancy - O'Hara' (with the spaces around the dashes), and what about the apostrophe in the surname? – Jonathan Leffler Commented Oct 24, 2010 at 19:00
  • @Jonathan: Yes I would like that as well, now that you mention it. – user188962 Commented Oct 24, 2010 at 19:04
  • Does this answer your question? Including a hyphen in a regex character bracket? – Karl Knechtel Commented May 3, 2024 at 20:14
Add a comment  | 

4 Answers 4

Reset to default 56

You have to escape the minus sign using backslash.

var alphaExp = /^[a-zA-ZåäöÅÄÖ\s\-]+$/;

To stop them from using it in the beginning or the end, try something like this:

var alphaExp = /^[a-zA-ZåäöÅÄÖ\s]+[a-zA-ZåäöÅÄÖ\s\-]*[a-zA-ZåäöÅÄÖ\s]+$/;

In many regex dialects, in a character class, a plain hyphen/minus needs to be first or last:

/^[-a-zA-ZåäöÅÄÖ\s]+$/
/^[a-zA-ZåäöÅÄÖ\s-]+$/

Negated character classes:

/^[^-a-zA-ZåäöÅÄÖ\s]+$/
/^[^a-zA-ZåäöÅÄÖ\s-]+$/

With close square bracket too, put the square bracket at the front and the hyphen at the end:

/^[]a-zA-ZåäöÅÄÖ\s-]+$/

And if you need to exclude both close square brackets and hyphens, then:

/^[^]a-zA-ZåäöÅÄÖ\s-]+$/

For the question, one interpretation might be: you want to insist on alphabetic characters around hyphens, and only want to allow spaces at the start and end, and you might want to allow apostrophes where you allow hyphens, and you want to avoid consecutive hyphens or apostrophes.

/^\s*[a-zA-ZåäöÅÄÖ]*([a-zA-ZåäöÅÄÖ]+[-'][a-zA-ZåäöÅÄÖ]+)*\s*$/

Warning: regex formally tested with Perl, not JavaScript.

Start of string, zero or more spaces; zero or more alphabetic characters; zero or more sequences of 'one or more alphabetic characters plus hyphen or apostrophe and one or more alphabetic characters', followed by end of string. You could slap an extra set of parentheses after the first \s* and before the second \s* to capture the whole name.

For Anna-nicole, the first alpha term would match Ann, and the other alpha term would match a-nicole. For Anonymous, the first term would match the whole string, the second would be empty. For O'Reilly, the first term would be empty and the second would match the whole string. Names such as "C--d" and "Who''Me" would be rejected (no repeated hyphen or apostrophe allowed). It would allow Smith-Jones-and-Son as a name, and Smith-And-O'Reilly. It won't allow leading or trailing hyphens or apostrophes.

If you wanted to allow 'first-name last-name', you'd need two lots of the 'core' of the regex above with \s+ in between. Etc.

/^[a-zA-ZåäöÅÄÖ\s]+[a-zA-ZåäöÅÄÖ\s\-]*[a-zA-ZåäöÅÄÖ\s\]+$/ 

should do it (there being a nasty edge case of a single character name, which this won't match. Is a single character name allowed or not?)

This may depend on the implementation.

In JavaScript the backslash will escape the minus sign in a character class, but in, for example, Oracle, only putting the minus sign first or last in the character class will do the trick.

发布评论

评论列表(0)

  1. 暂无评论