Can anyone help me with this regex? I need something which will ALLOW:
0-9 a-z A-Z spaces hyphens apostrophes
But disallow all other special characters.
I've got this, but it's not working:
"regex":"/^[0-9a-zA-Z/ /-'_]+$/",
Thanks for any help!
You should remove the double quotes around the regex:
"regex": /^[0-9a-zA-Z \-'_]+$/,
Also make sure you use backslashes to escape special characters, not forward slashes.
You could alternatively remove the outer forward slashes and pass it to the constructor of RegExp.
"regex" : new RegExp("^[0-9a-zA-Z \-'_]+$")
Which is equivalent to the /pattern/modifiers syntax (the second argument is an optional string of modifier characters). The \w character class matches alphanumeric characters, including underscore, so I think you can shorten your pattern quite a bit by using it.
^[\w \-']+$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With