The toString() method of the RegExp object in JavaScript returns a string representing the regular expression. This method overrides the Object.prototype.toString() method.
The syntax of RegExp.toString() method is:
toString()
This toString() method of RegExp returns a string representing the specified object. Overrides the Object.prototype.toString() method.
RegExp.toString() returns value of type String
.
In JavaScript, we can use the toString()
method to get the string representation of a regular expression.
For example,
regex
with the pattern /hello/i
.toString()
method to get the string representation of regex
.str
and we log it to the console.const regex = /hello/i;
const str = regex.toString();
console.log(str);
/hello/i
In JavaScript, we can use the toString()
method to get the string representation of a regular expression with the global flag.
For example,
regex
with the pattern /\d+/g
.toString()
method to get the string representation of regex
.str
and we log it to the console.const regex = /\d+/g;
const str = regex.toString();
console.log(str);
/\d+/g
In JavaScript, we can use the toString()
method to verify the pattern and flags of a regular expression.
For example,
regex
with the pattern /abc/i
.toString()
method to get the string representation of regex
.str
and we log it to the console.const regex = /abc/i;
const str = regex.toString();
console.log(str);
/abc/i
In this JavaScript tutorial, we learned about toString() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.