Casey Chu
Casey Chu

Reputation: 25463

Special Characters in Regex Brackets

Let's say I want to match one of the following characters: a, b, c, or + (in JavaScript). Do I need to escape the +? Is it /[abc+]/ or /[abc\+]/? Both work in my limited selection of test browsers. Which is (more) correct?

Upvotes: 7

Views: 3368

Answers (2)

Sarfraz
Sarfraz

Reputation: 382696

No need to escape the + in character class [xxx]:

/[abc+]/

Upvotes: 2

Aistina
Aistina

Reputation: 12683

Regex reference

Under character classes:

Any character except ^-]\ add that character to the possible matches for the character class.

In other words, you don't have to escape the +.

Upvotes: 10

Related Questions