I am limiting my input to only accept this characters:
- apostrophe ‘
- open parenthesis (
- close parenthesis )
- comma ,
- hyphen –
- period .
- alphanumeric A-Z a-z 0-9
- space
I come up with this regex:
/^['(),-.A-Za-z0-9\s]+$/
Do I violate any regularization order in here? I just want to have the order of regex based on order I stated on the bullets above.
Tweaking the regex in the question comment by @Mark Linus:
/^[-'(),.\w\s]+$/(moving the hyphen to the beginning of the character class)The reason: In a regex character class,
-Specifies a range of characters, unless it’s the first or the last character.,-.means any characters from,to.inclusive. As it happens,-is the single character between those two (based on ASCII value), so it works out the same in this case. But it’s probably not a good idea to put a hyphen anywhere in a character class except the beginning or the end unless you mean for it to specify a range.