Regex Javascript 0-9a-zA-Z plus spaces, commas, etc.
4 answers
You can also remove the outer slashes and pass it to the constructor RegExp
.
"regex" : new RegExp("^[0-9a-zA-Z \-'_]+$")
Which is equivalent to syntax /pattern/modifiers
(the second argument is an optional modifier character string). The character class \w
matches alphanumeric characters, including the underscore, so I think you can shorten your pattern a bit by using that.
^[\w \-']+$
+3
source to share
If you want to match underscores as well as alphanumeric characters (as your code suggests), you can use
"regex": /^[\w '-]+$/,
Also, check out this regex checking tool online .
0
source to share