Open In App

JavaScript RegExp compile() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 = "GeeksforGeeks is the computer"
        + " science portal for geeks.";
 
    let patt = /geek/g;
    let str2 = str.replace(patt, "GFG");
 
    console.log("Initial:", str2);
 
    patt = /(Geeks)/gi;
    patt.compile(patt);
 
    str2 = str.replace(patt, "GEEKS");
    console.log("After using compile():", str2);
}
 
geek();


Output

Initial: GeeksforGeeks is the computer science portal for GFGs.
After using compile(): GEEKSforGEEKS is the computer science portal for GEEKS.

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.  


Last Updated : 18 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads