poo
poo

Reputation: 1115

How to check special characters with Regular Expressions?

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.

Upvotes: 1

Views: 2523

Answers (1)

kennytm
kennytm

Reputation: 523304

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

Upvotes: 1

Related Questions