Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript RegExp constructor

Improve Article
Save Article
Like Article
  • Last Updated : 06 Jan, 2023
Improve Article
Save Article
Like Article

The RegExp constructor in JavaScript is used to return the function that created the RegExp object’s prototype i.e. the construction function for an object. It returns the different references for various JavaScript types:  

  • Regular Expression: The constructor property returns function RegExp() { [native code] } for regular expressions.
  • Numbers: The constructor property returns function Number() { [native code] } for JavaScript numbers.
  • Strings: The constructor property returns function String() { [native code] } for JavaScript strings.

Syntax:  

RegExpObject.constructor

Example-1: This example returns the type of constructor on variable regex4.  

html




<h1 style="color:green">
    GeeksforGeeks
</h1>
<h2>
    RegExp Constructor
</h2>
  
<p>
    String: ee@128GeeeeK
</p>
  
<button onclick="geek()">
    Click it!
</button>
<p id="app"></p>
  
<script>
    function geek() {
        var str1 = "ee@128GeeeeK";
        var regex4 = new RegExp("e{2, }", "gi");
        var rex = regex4.constructor;
        document.getElementById("app").innerHTML =
            " Constructor: " + rex;
    }
</script>

Output: 

JavaScript RegExp constructor

JavaScript RegExp constructor

Example-2: This example returns the type of constructor on variable match4. 
 

html




<h1 style="color:green">
    GeeksforGeeks
</h1>
<h2>
    RegExp Constructor
</h2>
  
<p>
    String: GeeeeK@128
</p>
  
<button onclick="geek()">
    Click it!
</button>
<p id="app"></p>
  
<script>
    function geek() {
        var str1 = "GeeeeK@128";
        var regex4 = new RegExp("e{2, }", "gi");
        var replace = "$";
        var match4 = str1.replace(regex4, replace);
        var rx = match4.constructor;
        document.getElementById("app").innerHTML =
            " Constructor: " + rx;
    }
</script>

Output: 

JavaScript RegExp constructor

JavaScript RegExp constructor

Supported Browsers: The browsers supported by RegExp constructor are listed below: 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 5 and above
  • Safari 1 and above

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.  


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!