Regular Expression Object

An object that contains a regular expression pattern along with flags that identify how to apply the pattern.

Syntax 1

re = /pattern/[flags]

Syntax 2

re = new RegExp("pattern",["flags"]) 


Required. The variable name to which the regular expression pattern is assigned.
Required. The regular expression pattern to use. If you use Syntax 1, delimit the pattern by "/" characters. If you use Syntax 2, enclose the pattern in quotation marks.
Optional. Enclose flag in quotation marks if you use Syntax 2. Available flags, which may be combined, are:
  • g (global search for all occurrences of pattern)
  • i (ignore case)
  • m (multiline search)


The Regular Expression object should not be confused with the global RegExp object. Even though they sound the same, they are separate and distinct. The properties of the Regular Expression object contain only information about one particular Regular Expression instance, while the properties of the global RegExp object contain continually updated information about each match as it occurs.

Regular Expression objects store patterns used when searching strings for character combinations. After the Regular Expression object is created, it is either passed to a string method, or a string is passed to one of the regular expression methods. Information about the most recent search performed is stored in the global RegExp object.

Use Syntax 1 when you know the search string ahead of time. Use Syntax 2 when the search string is changing frequently, or is unknown, such as strings taken from user input.

The pattern argument is compiled into an internal format before use. For Syntax 1, pattern is compiled as the script is loaded. For Syntax 2, pattern is compiled just before use, or when the compile method is called.


The following example illustrates the use of the Regular Expression object by creating an object (re) containing a regular expression pattern with its associated flags. In this case, the resulting Regular Expression object is then used by the match method:

function MatchDemo(){
   var r, re;                     //Declare variables.
   var s = "The rain in Spain falls mainly in the plain";
   re = new RegExp("Spain","i");  //Create regular expression object.
   r = s.match(re);               //Find a match within string s.
   return(r);                     //Return results of match.


lastIndex Property | source Property


compile Method | exec Method | test Method


Version 3

See Also

RegExp Object | Regular Expression Syntax | String Object