Including a hyphen in a regex character bracket?
Solution 1
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._-]+$/
Solution 2
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.)
Solution 3
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.
Solution 4
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.
Solution 5
\-
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
ParoX
Updated on April 10, 2021Comments
-
ParoX about 3 years
$.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--
-
ParoX over 13 yearsVery nice. :D I prefer the readability though ( I guess \w would be easy for some experts though)
-
Ross Presser almost 9 yearsPutting the hyphen last does NOT work with some Microsoft tools, like Microsoft SQL Server Management Studio. Escaping they hyphen does work.
-
Ross Presser almost 9 yearsPutting the hyphen last does NOT work with some Microsoft tools, like Microsoft SQL Server Management Studio. Escaping they hyphen does work.
-
Matt almost 8 years@SabaAhang because if it's at the beginning or the end it can't be between two other characters to create a range of characters (e.g.
[0-9]
) -
AbstractVoid over 7 years@SabaAhang the following documentation page mentions that hyphen can be unescaped in the beginning or end of character brackets: regular-expressions.info/charclass.html
-
Topman almost 4 yearsI needed on the server side using C#. It worked. Thank you.
-
MarredCheese over 3 years@Topman Better yet (for C#), use verbatim string literals for server-side regex, filenames, etc. to avoid confusion (
@"\"
instead of"\\"
).