Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript compile() method

JavaScript compile() method

RegExp Object Reference JavaScript RegExp Object


Definition and Usage

The compile() method is used to compile a regular expression during script execution.

The compile() method can also be used to change and recompile a regular expression.

Syntax

RegExpObject.compile(regexp,modifier)

Parameters Description
regexp The regular expression.
modifier Specifies the match type. "g" is for global match, "i" is for case-insensitive, "gi" is for global, case-insensitive match.

Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All browsers except Opera support the compile() method.


Example

Example

Globally search for "man" in the string and replace it with "person". Then use the compile() method to change the regular expression and replace "man" or "woman" with "person":

The example above produces the following output:

Every person in the world! Every woperson on earth!

Every person in the world! Every person on earth!

Try it »


RegExp Object Reference JavaScript RegExp Object