The Regexp Type

JavaScript supports regular expressions through the RegExp type. A regular expression is created using a combination of a pattern and these flags. The syntax to declare a RegExp type.

var expression = /pattern/flags;

The pattern can be any simple or complicated regular expression. The pattern may have character classes, quantifiers, grouping, lookaheads, and backreferences. The flags tells how the expression should behave.

Three supported flags represent matching modes:

FlagMeaning
gthe pattern will be applied to all of the string, does not stop for the first found.
icase-insensitive mode.
mmultiline mode.

// Match all instances of "oo" in a string. 
var pattern1 = /oo/g;

//Match the first instance of "doo" or "roo", regardless of case. 
var pattern2 = /[dr]oo/i;

// Match all three-character combinations ending with "oo", regardless of case. 
var pattern3 = /.oo/gi;

The following metacharacters must be escaped when used as part of the pattern.

( [ { \ ^ $ | ) ] } ? * + .

// Match the first instance of "doo" or "roo", regardless of case. 
var pattern1 = /[dr]oo/i;

// Match the first instance of "[dr]oo", regardless of case. 
var pattern2 = /\[dr\]oo/i;

// Match all three-character combinations ending with "oo", regardless of case.  
var pattern3 = /.oo/gi;

// Match all instances of ".oo", regardless of case. 
var pattern4 = /\.oo/gi;
Home 
  JavaScript Book 
    Essential Types  

REGEXP:
  1. The Regexp Type
  2. RegExp constructor
  3. RegExp Instance Properties
  4. RegExp's exec() does the matching.
  5. RegExp's test()
  6. RegExp's toLocaleString() and toString()
  7. RegExp Constructor Properties