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;




















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions