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]+$/
.
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
}
Try 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.