How to use compile in JavaScript

This article describe about compile() Method in JavaScript.
  • 1355

compile() Method in JavaScript

It is used to compile a regular expression during execution of a script.

And it is also used to change and recompile a regular expression.

Syntax

RegExpObject.compile(regexp,modifier)

 

Parameter Description
regexp A regular expression
modifier Define the Type of matching "g" for global. "i" for case sensitive and "gi" for global case sensitive.

Example

<!DOCTYPE html>

<html>

<body>

<script type="text/javascript">

    var str = "Everyone live in the world! Every womanlive on earth!";

    var pattern = /live/g;

    var str2 = str.replace(pattern, "person");

    document.write(str2 + "<br />");

    pattern = /(wo)?live/g;

    pattern.compile(pattern);

    str2 = str.replace(pattern, "person");

    document.write(str2);

</script>

</body>

</html>

Output

pic1.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.