JavaScript RegExp compile() Method

The compile() Method in JavaScript is used to compile the regular expression while executing of script i.e compile the regular expression. It is also used to recompile and change the regular expression.

Syntax:

RegExpObject.compile(RegExp, modifier)

Parameters:

  • RegExp: RexExp refers to the regular expression.
  • modifier: It is used to specify the type of matching. 

Note: This method is deprecated now.

Example: This example changes the initial string and then again changes it on using compile() method.  

Javascript




function geek() {
    let str = "w3wiki is the computer"
        + " science portal for Beginner.";
 
    let patt = /geek/g;
    let str2 = str.replace(patt, "GFG");
 
    console.log("Initial:", str2);
 
    patt = /(Beginner)/gi;
    patt.compile(patt);
 
    str2 = str.replace(patt, "Beginner");
    console.log("After using compile():", str2);
}
 
geek();


Output

Initial: w3wiki is the computer science portal for GFGs.
After using compile(): w3wiki is the computer science portal for Beginner.

Supported Browsers: The browsers supported by JavaScript compile() Method are listed below: 

  • Google Chrome 
  • Apple Safari
  • Mozilla Firefox
  • Opera 
  • Internet Explorer

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Contact Us