JavaScript RegEx to allow only AlphaNumeric Characters and some special characters
In a textarea field, i want to allow only alphanumeric characters, -(hyphen),/(forw开发者_运维知识库ard slash), .(dot) and space. I have gone through similar questions but one way or the other, my exact requirements seem to differ. So, below is the regex i've come up with after reading answers give by members:
/^[a-z0-9\-\/. ]+$/i
I've tested the regex and so far it seems to work but i want to double check. Please verify as to whether the above regex fulfills my requirements.
You do too much escaping
/^[a-z0-9/. -]+$/i
In a character class, only [
, ]
, \
, -
and ^
have special meaning, the ^
even only when it is the first character and the -
only if it is between characters.
To match a literal ^
just put it into any position but the first. To match a -
literally, don't put it between characters (i.e., at the start or at the end).
Escaping things like the /
, .
or $
is never necessary.
精彩评论