tags:

views:

41

answers:

1

In javascript I check for some characters 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.

+1  A: 

_ 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.)

KennyTM
You don't actually need to escape the `/` inside square brackets
Andy E
Like this then = alias: /^[/w/d\/_]{2,35}$/
poo
@Andy: You need to escape it because the delimiter is `/`.
KennyTM
@KennyTM: It doesn't matter. `/test[/]/i` is a perfectly valid regular expression in JavaScript.
Andy E