Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptJavaScript RegExp compile() Method

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 = "neveropen is the computer"
        + " science portal for neveropen.";
 
    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: neveropen 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.  

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments