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?
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.
[[\]] will match either bracket. In some regex dialects (e.g. grep) you can omit the backslash before the ] if you place it immediately after the [ (because an empty character class would never be useful): [][] .
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 +
.
No need to escape the +
in character class [xxx]
:
/[abc+]/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With