ParoX
ParoX

Reputation: 5933

Including a hyphen in a regex character bracket?

$.validator.addMethod('AZ09_', function (value) { 
    return /^[a-zA-Z0-9.-_]+$/.test(value); 
}, 'Only letters, numbers, and _-. are allowed');

When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --

Upvotes: 103

Views: 164913

Answers (7)

Daniel Abril
Daniel Abril

Reputation: 454

Desipite being the accepted answer fine in most of the cases, working with field HTML/CSS validation I've found some problems even escaping, not only the hyphen, but also other characters such as the space (\s) or the dot (.). Normally a single scape \- would do the work, but in validation seems to require double escaping them, i.e. escape the the escaping backslash: \\-

12/07/2023 update: Having to double escape REGEX reserved chars seems related to the fact of using a JS compiler (in my case Ember-cli). The single escape works fine in a codepen.

Hope this helps someone.

Upvotes: 1

akhouri
akhouri

Reputation: 3155

Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.

In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/

In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.

Upvotes: 5

Radu Simionescu
Radu Simionescu

Reputation: 4676

A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression

Upvotes: 1

The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.

  • In a server side string: \\-
  • On the client side: \-
  • In regex (covers): -

Or you can simply put at the and of the [] brackets.

Upvotes: 4

Guffa
Guffa

Reputation: 700212

Escaping the hyphen using \- is the correct way.

I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.

(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)

Upvotes: 41

T.J. Crowder
T.J. Crowder

Reputation: 1074138

\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838066

Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:

/^[a-zA-Z0-9._-]+$/

Upvotes: 152

Related Questions