RegExp

RegExp.prototype.toString

Returns a string representing the regular expression

Syntax

JavaScript
regexp.toString()

Return Value

A string of the form /pattern/flags

Examples

Basic Usage
const regex = /hello/gi;
console.log(regex.toString()); // '/hello/gi'
Practical Example
const regex = new RegExp('\\d+', 'g');
console.log(regex.toString()); // '/\d+/g'
Advanced Usage
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

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.