Include the “minus-sign” into this regular express

2019-01-23 11:50发布

问题:

I have this regex:

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

This is for a name-field in a form validation.

I need to make it possible to type names like this "Anna-nicole" (note the minus sign). Currently the minus sign causes error.

I need to remake this regex so that a minus sign can be included in the name; preferably I should make it so that the name also cannot start with a minus sign, or end with one. Only minus signs between "words, or names" should be allowed.

Does anybody know how to do this?

BTW, it is JavaScript.

回答1:

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]+$/;


回答2:

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.



回答3:

/^[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?)



回答4:

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.