JavaScript tutorial:
compile method

 

Applies To: Regular Expression Object

The compile method compiles a regular expression into an internal format.

Syntax

rgexp.compile(pattern)

The compile method syntax has these parts:

Part

Description

rgexp

Required. A Regular Expression object. Can be a variable name or a literal.

pattern

Required. A string expression containing a regular expression pattern to be compiled.

Return value

Returns no value.

Remarks

The compile method converts pattern into an internal format for faster execution. This allows for more efficient use of regular expressions in loops, for example.

The following example illustrates the use of the compile method:

function CompileDemo()
{
var s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp"
// Create regular expression for uppercase only.
var r = new RegExp("[A-Z]", "g");
var a = s.match(r) // Find matches.
document.write(a);
// Compile regular expression for lowercase only.
r.compile("[a-z]", "g");
var a = s.match(r) // Find matches.
document.write(a);
}
document.write(CompileDemo());
To run the code, paste it into JavaScript Editor, and click the Execute button.

See Also: Regular Expression Object Methods, Regular Expression Object Properties, Regular Expression Syntax