Escape meta data in regular expression in Javascipt
Description
We have to escape all metacharacters by using \
when using them as part of the pattern.
The metacharacters are as follows:. ( [ { \ ^ $ | ) ] } ? * +
.
Example
// Match the first instance of "bat" or "cat", regardless of case.
var pattern1 = /[bc]at/i;
//from ww w.j a v a 2s. c o m
// Match the first instance of "[bc]at", regardless of case.
var pattern2 = /\[bc\]at/i;
// Match all three-character combinations ending with "at", regardless of case.
var pattern3 = /.at/gi;
// Match all instances of ".at", regardless of case.
var pattern4 = /\.at/gi;