I am having trouble forming regular expression containing all alphanumeric characters and one or two specific characters, such us _
or -
.
This expression works for all alphanumeric characters /^[0-9a-zA-Z]+$/
.
I am having trouble forming regular expression containing all alphanumeric characters and one or two specific characters, such us _
or -
.
This expression works for all alphanumeric characters /^[0-9a-zA-Z]+$/
.
- You need a regex to find another regex? – Gabber Commented Aug 29, 2012 at 10:34
2 Answers
Reset to default 2Try this:
/^[0-9a-zA-Z-_]+$/
If you enter the dash sign "-" at a position where it can be interpreted as a range such as _- it would mean any characters matching _ or above in the ascii table.
Add the special characters inside the square brackets
/^[0-9a-zA-Z_-]+$/
To use this regex in javascript use this code (yourPhrase
is the string you check vs the regexp)
var rexp = /^[0-9a-zA-Z_-]+$/
if(rexp.test(yourPhrase)){
//code to handle the test
}