Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | RegExp toString() Method

  • Last Updated : 30 Sep, 2021

The RegExp toString() Method in JavaScript is used to return the string value of the regular expression.
Syntax: 
 

RegExpObject.toString()

Example 1: This example returns the string value of the regular expression. 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript RegExp toString Method
    </title>
</head>
 
<body style="text-align:center">
     
    <h1 style="color:green">GeeksforGeeks</h1>
     
    <h2>RegExp toString() Method</h2>
     
     
<p>Input: RegExp([a-c]", "gi")</p>
 
     
    <button onclick="geek()">Click it!</button>
     
    <p id="app"></p>
 
     
    <script>
        function geek() {
            var regex = new RegExp("[a-c]", "gi");
            var rex = regex.toString();
            document.getElementById("app").innerHTML =
            "String Value: " + "<b>" + rex + "</b>";
        }
    </script>
</body>
 
</html>                   

Output: 
Before clicking the button: 
 

tostring

After clicking the button: 
 

tostring

Example 2: This example returns the string value of the regular expression. 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript RegExp toString Method
    </title>
</head>
 
<body style="text-align:center">
     
    <h1 style="color:green">GeeksforGeeks</h1>
     
    <h2>RegExp toString() Method</h2>
     
     
<p>Regular Expression: /[a-k]/g</p>
 
     
    <button onclick="geek()">Click it!</button>
     
    <p id="app"></p>
 
     
    <script>
        function geek() {
            var regex = /[a-k]/g;
            var rex = regex.toString();
            document.getElementById("app").innerHTML =
            "String Value: " + "<b>" + rex + "</b>";
        }
    </script>
</body>
 
</html>                   

Output: 
Before clicking the button: 
 

tostring

After clicking the button: 
 

tostring

Supported Browsers: The browsers supported by RegExp toString() Method 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

 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!