views:

48

answers:

2

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?

+2  A: 

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

/[abc+]/
Sarfraz
+7  A: 

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 +.

Aistina
Or `?`,`*` and `.` which a lot of people (including myself) often seem to do out of habit as well.
gnarf