Compile Regular Expressions Method

The Compile Regular Expressions method modifies the pattern and attributes for the current instance of a regular expression object. It allows you to use a regular expression instance multiple times with modifications to the characteristics of this instance. You use it with a regular expression that the constructor function creates. You do not use this method with the literal notation.

Format

regexp.compile(pattern[, attributes])

The following table describes the arguments for the Compile Regular Expressions method.

Argument Description

pattern

A string that contains a new regular expression.

attributes

A string that contains new attributes. If you include the attributes argument, then this string must be empty, or it must contain one or more of the following characters:

  • i. Sets the ignoreCase property to true.

  • g. Sets the global property to true.

  • m. Sets the multiline property to true.

Example

The following example uses the Compile Regular Expressions method:

var regobj = new RegExp("now");
// use this RegExp object
regobj.compile("r*t");
// use it some more
regobj.compile("t.+o", "ig");
// use it some more

For more information, see the following topics: