RegExp.prototype.toString
Returns a string representing the regular expression
Syntax
regexp.toString()Return Value
A string of the form /pattern/flags
Examples
const regex = /hello/gi;
console.log(regex.toString()); // '/hello/gi'const regex = new RegExp('\\d+', 'g');
console.log(regex.toString()); // '/\d+/g'const flags = /test/gim;
console.log(flags.toString()); // '/test/gim'Understanding RegExp.prototype.toString
The RegExp.prototype.toString method in JavaScript returns a string representing the regular expression. It belongs to the RegExp object and is one of the most widely used methods for working with regexp values in modern JavaScript and TypeScript applications.
The method signature is regexp.toString(). When called, it returns a string of the form /pattern/flags. Understanding when and how to use toString() helps you write more expressive, readable code.
Common use cases for RegExp.prototype.toString include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like regexp-test, regexp-exec, regexp-source, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for RegExp.prototype.toString is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
RegExp.prototype.testExecutes a search for a match between a regular expression and a specified string, returning true or false
RegExp.prototype.execExecutes a search for a match in a specified string and returns a result array or null
RegExp.prototype.sourceReturns a string containing the source text of the regex pattern, without the forward slashes or flags
More RegExp Methods
Other methods in the RegExp object
Related Tools
More RegExp Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.