compile

Compiles a regular expression object during execution of a script.

Applies to

RegExp

Syntax

regexp.compile(pattern, flags)

Parameters

regexp

The name of he regular expression. It can be a variable name or a literal.

pattern

A string containing the text of the regular expression.

flags

(Optional) If specified, flags can have one of the following 3 values:

Description

Use the compile method to compile a regular expression created with the RegExp constructor function. This forces compilation of the regular expression once only which means the regular expression isn't compiled each time it is encountered. Use the compile method when you know the regular expression will remain constant (after getting its pattern) and will be used repeatedly throughout the script.

You can also use the compile method to change the regular expression during execution. For example, if the regular expression changes, you can use the compile method to recompile the object for more efficient repeated use.

Calling this method changes the value of the regular expression's source, global, and ignoreCase properties.