Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The toString() method of RegExp instances returns a string representing this regular expression.
None.
A string representing the given object.
The RegExp object overrides the toString() method of the Object object; it does not inherit Object.prototype.toString(). For RegExp objects, the toString() method returns a string representation of the regular expression.
In practice, it reads the regex's source and flags properties and returns a string in the form /source/flags. The toString() return value is guaranteed to be a parsable regex literal, although it may not be the exact same text as what was originally specified for the regex (for example, the flags may be reordered).
The following example displays the string value of a RegExp object:
Since toString() accesses the source property, an empty regular expression returns the string "/(?:)/", and line terminators such as \n are escaped. This makes the returned value always a valid regex literal.
| ECMAScript® 2027 Language Specification # sec-regexp.prototype.tostring |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Aug 3, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.