Skip to content Skip to sidebar Skip to footer

Include The Hyphen Into This Regular Expression, How?

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' (n

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

Solution 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*$/

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.

Solution 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?)

Solution 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.

Post a Comment for "Include The Hyphen Into This Regular Expression, How?"