All meta characters must be escaped when used as part of the pattern.
The meta characters are as follows:
( [ { \ ^ $ | ) ] } ? * + .
Each meta character has one or more uses in regular-expression syntax.
We must be escaped by a backslash when you want to match the character in a string.
Here are some examples.
Match the first instance of "bat" or "cat", regardless of case.
let pattern1 = /[bc]at/i;
Match the first instance of "[bc]at", regardless of case.
let pattern2 = /\[bc\]at/i;
Match all three-character combinations ending with "at", regardless of case.
let pattern3 = /.at/gi;
Match all instances of ".at", regardless of case.
let pattern4 = /\.at/gi;
In this code, pattern1 matches all instances of "bat" or "cat", regardless of case.
To match "[bc]at" directly, both square brackets need to be escaped with a backslash, as in pattern2
.
In pattern3
, the dot indicates that any character can precede "at" to be a match.
To match ".at", then the dot needs to be escaped, as in pattern4.