JavaScript | RegExp constructor

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 reference 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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align:center">
    <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>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
constructor
After clicking the button:
constructor

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align:center">
    <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>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
constructor
After clicking the button:
constructor

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

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


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.