RegExp.prototype.toString() returns a readable string of the regex in /pattern/flags form. It does not search text — it only describes the RegExp for logs, errors, and debugging.
Remember
regex.toString() → "/pattern/flags"
/hello/gi.toString() → "/hello/gi"
new RegExp("a+b", "i") → "/a+b/i"
source → pattern only ("hello")
flags → flags only ("gi")
toString → both with slashes ("/hello/gi")
Does not match text. Does not change lastIndex.
Use
Prefer
Log / display the regex
regex.toString()
Pattern text only
regex.source
Clone a regex
new RegExp(regex.source, regex.flags)
Approach
How to Use toString()
Call it on any RegExp when you need a string description.
Start with a regex literal or new RegExp(…).
Call regex.toString() (or coerce with String(regex)).
Use the result in logs, error messages, or UI debug panels.
For rebuilding a regex, use source + flags instead of parsing the string.
Piece
Required?
Role
RegExp object
Yes
Value to describe
Arguments
None
toString() takes no parameters
Return value
—
Always a string like /pat/flags
Pseudocode
Pseudocode
regex = /pattern/flags
result = "/" + regex.source + "/" + regex.flags
return result
// String(regex) and `${regex}` also call toString()
// No matching. No lastIndex updates.
Reference
Cheat Sheet
Keep these facts handy while logging or debugging regexes.
Topic
Detail
Syntax
regex.toString()
Shape
/pattern/flags
vs source
Pattern only, no slashes
vs flags
Flag letters only (gi, …)
Coercion
String(regex), `${regex}`
Matches text?
No — description only
Standard
ES3+ (universal)
Try it
Live Preview
Pick a regex and see toString(), source, and flags side by side.
Browser demo of RegExp.prototype.toString — same idea as the examples below.
Validate first.test() checks the value against the rule.
On failure, describe the rule.toString() shows exactly which pattern was expected.
Templates coerce too.${zipRule} would call toString() automatically.
Why it helps. Logs and errors become self-explanatory without opening DevTools.
Edge Cases & Pitfalls
Check these before relying on toString() in real projects.
clone
Do not parse toString() to rebuild a regex
Use new RegExp(regex.source, regex.flags). Parsing the slash form breaks on patterns that contain /.
escape
Escapes may look different from your source code
source (and therefore toString()) shows the engine’s stored pattern, which can differ slightly from how you typed the literal.
role
It never matches text
toString() is for description only. Use test() or exec() to search a string.
empty
Empty pattern still has slashes
/(?:)/.toString() or an empty constructor pattern still returns a slash-wrapped string such as /(?:)/.
Compatibility
Browser & Runtime Support
RegExp.prototype.toString() is part of core JavaScript (ES3+). It works everywhere without polyfills.
✓ Baseline · ES3+
RegExp.prototype.toString()
Supported in every modern and legacy browser, plus all Node.js versions. Universal way to stringify a RegExp.
99%Universal API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
toString()Excellent
Bottom line: Safe everywhere. Use toString() for logs and errors; use source and flags when you need the parts separately.
Analysis
Performance Notes
Approach
Cost
Best for
regex.toString()
Tiny string build
Logs, errors, debug UI
regex.source / .flags
Property reads
Cloning or inspecting parts
Parse toString() output
Fragile
Avoid — use source + flags
toString() is cheap and fine for logging. Do not call it in hot match loops — matching itself is the expensive part, and stringifying every time only adds noise.
Remember
Key Takeaways
Role:toString() returns /pattern/flags as a string.
Parts: use source and flags when you need them separately.
Coercion:String(regex) and templates call toString() too.
Clone:new RegExp(source, flags) — do not parse the slash string.
One line:toString() turns a RegExp into a clear /pattern/flags label for humans — not a tool for matching.
Frequently Asked Questions
toString() returns a string in the form /pattern/flags—for example, /hello/gi. It includes the opening and closing slashes and any flags appended after the closing slash.
Yes. String(regex) and regex.toString() both use the same conversion. Template literals like `${regex}` also call toString() under the hood.
source returns only the pattern text without slashes or flags. toString() wraps source and flags together as a literal-style string: `/` + source + `/` + flags.
Mostly for debugging and logs. Prefer source and flags when building a new RegExp programmatically: new RegExp(regex.source, regex.flags).
No. toString() only describes the RegExp object. It does not search strings or change lastIndex.
RegExp.prototype.toString() has been part of JavaScript since ES3. It works in every browser and Node.js version.