Include the hyphen into this regular expression, how?

98,916

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

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.

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

Share:
98,916
Admin
Author by

Admin

Updated on July 12, 2022

Comments

  • Admin
    Admin almost 2 years

    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 hyphen). Currently the hyphen causes error.

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

    Does anybody know how to do this?

    BTW, it is JavaScript.

  • Admin
    Admin over 13 years
    Will this stop them from using minus-sign in beginning or end of the field?
  • Admin
    Admin over 13 years
    Thanks Jonathan, but should I combine all these? Dont really know what they all mean, just tell me which one to chose please :)
  • Jonathan Leffler
    Jonathan Leffler over 13 years
    @Camran: Choose the one you need - which is probably either of the first two. The second two deal with a negated character class, where the first character after the '[' is '^'. The last is even more esoteric. However, they don't any of them deal with the more complex issues of ensuring that dash only appears between words, not associated with spaces, etc. I'm half-tempted to delete the answer - but the information about where to place dashes in character classes in regular expressions is still useful (I hope), even though it doesn't address the bigger (and much more) complex issues.
  • Martin Clemens Bloch
    Martin Clemens Bloch almost 11 years
    Replace the "+"-sign with {1} to prevent ONLY the first and last characters from being "-" or the first + with {5} if it should be allowed after 5 chars. addedbytes.com/cheat-sheets/download/…
  • Geoffrey Booth
    Geoffrey Booth almost 10 years
    Care to give the OP an example of what you mean? Such as by fixing their code?
  • redreinard
    redreinard over 9 years
    It might be worth noting that a) the minus should be placed at the beginning or end and b) that escaping it is not required in many regex implementations
  • Ooker
    Ooker over 4 years
    is there a way to escape all of them at once, to improve readability?