JavaScript | RegExp toString() Method
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.
<!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:

After clicking the button:

Example 2: This example returns the string value of the regular expression.
<!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:

After clicking the button:

Supported Browsers: The browsers supported by RegExp toString() Method are listed below:
- Google Chrome
- Apple Safari
- Mozilla Firefox
- Opera
- Internet Explorer
Recommended Posts:
- JavaScript | RegExp test() Method
- JavaScript | RegExp \S Metacharacter
- JavaScript | RegExp \d Metacharacter
- JavaScript | RegExp {X,} Quantifier
- JavaScript | RegExp \v Metacharacter
- JavaScript | RegExp \s Metacharacter
- JavaScript | RegExp ^ Quantifier
- JavaScript | RegExp . Metacharacter
- JavaScript | RegExp $ Quantifier
- JavaScript | RegExp {X,Y} Quantifier
- JavaScript | RegExp g Modifier
- JavaScript | RegExp i Modifier
- JavaScript | RegExp [^abc] Expression
- JavaScript | RegExp m Modifier
- JavaScript | RegExp [^0-9] Expression
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.



