开发者

How to check special characters with Regular Expressions?

In javascript I check for some characters开发者_C百科 but I want to allow underscores and slashes but I don't know how.

    alias: /^[a-z-Z0-9]{2,35}$/

How to put / and _ so it has not special meaning to Regexes.


_ has no special meaning at all in Regex.

And if a character has special meaning, you can use \ to "despecialize" it.

alias: /^[a-zA-Z0-9_\/]{2,35}/

(BTW, you can use \w which means [a-zA-Z0-9_], i.e. /^[\w\/]{2,35}/. The \ in \w turns a normal character w to have a special meaning.)

(Edit: Inside the […] the / will not be recognized as a delimiter so it is safe to use /^[\w/]{2,35}/. Thanks Andy E for showing this.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜