Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include the "minus-sign" into this regular expression, how?

People also ask

How do you add a minus sign in regex?

- the minus sign indicates a range in a character class (when it is not at the first position after the "[" opening bracket or the last position before the "]" closing bracket. Example: "[A-Z]" matches any uppercase character. Example: "[A-Z-]" or "[-A-Z]" match any uppercase character or "-".

How do you include special characters in regular expressions?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \.

What does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.

How do you include a dash in regex?

The quantifier notations In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.


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