开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜