RegExp.prototype.compile(pattern, flags?) recompiles an existing RegExp in place with a new pattern and optional flags. It returns the same object after mutating it.
Remember
regex.compile(pattern, flags?)
mutates the same RegExp object
returns that same object
legacy / non-standard
prefer:
regex = new RegExp(pattern, flags)
Use
Prefer
Change pattern (legacy)
regex.compile(p, f) — learn only
Change pattern (modern)
regex = new RegExp(p, f)
Create once
/pattern/flags or new RegExp
Approach
How to Use compile()
Study the legacy call, then replace it with new RegExp in real projects.
Start with an existing RegExp object.
Call regex.compile(pattern, flags) — flags are optional.
The same object now has the new source / flags.
For new code, assign regex = new RegExp(pattern, flags) instead.
Piece
Required?
Role
RegExp object
Yes
Receiver to recompile
pattern
Yes
New pattern string (or RegExp)
flags
Optional
New flags string (e.g. "gi")
Pseudocode
Pseudocode
regex = existing RegExp
// legacy:
regex.compile(pattern, flags?)
// same object updated in place
// lastIndex usually reset to 0
// modern replace:
regex = new RegExp(pattern, flags)
Reference
Cheat Sheet
Keep these facts handy when reading legacy regex code.
Topic
Detail
Syntax
regex.compile(pattern, flags?)
Returns
Same RegExp object (mutated)
Mutates?
Yes — in place
Status
Deprecated / non-standard
Modern replace
new RegExp(pattern, flags)
lastIndex
Typically reset to 0
Try it
Live Preview
Recompile a RegExp (when available) and compare with new RegExp.
Browser demo — learn the legacy API; prefer new RegExp in real apps.
Alias the object.ref and regex point to the same RegExp.
Call compile. Pattern becomes "xyz" on that object.
Return value.result === regex is true — not a new instance.
Shared refs update.ref.source is also "xyz".
Example 3 — Prefer new RegExp
Compare the legacy call with the modern, standard approach.
JavaScript
let regex = /cat/i;
// Legacy — do not use in new code
if (typeof regex.compile === "function") {
regex.compile("dog", "g");
}
// Prefer — standard and clear
regex = new RegExp("dog", "g");
console.log(regex.source); // "dog"
console.log(regex.flags); // "g"
console.log(regex.test("dog")); // true
Legacy path.compile() mutates the old object (if present).
Modern path.new RegExp("dog", "g") creates a fresh standard RegExp.
Reassign.regex = … points at the new object — clear ownership.
Rule of thumb. Learn compile() to read old code; ship new RegExp.
Edge Cases & Pitfalls
Check these before you copy compile() from old tutorials.
deprecated
Not in the ECMAScript standard
Some engines still ship it for compatibility. Others may not. Always feature-detect or avoid it.
mutation
Shared references change together
Because the object is mutated, every alias of that RegExp sees the new pattern — easy to surprise callers.
lastIndex
lastIndex usually resets
After recompile, search position is typically 0. Do not assume a previous global search position remains.
replace
Prefer new RegExp
Creating a new RegExp is standard, clearer, and works in every modern environment.
Compatibility
Browser & Runtime Support
RegExp.prototype.compile() is legacy / non-standard. Some engines (e.g. V8 / Chrome / Node) still expose it; do not depend on it in new projects.
✓ Deprecated · Non-standard
RegExp.prototype.compile()
May exist for backward compatibility in Chromium-based environments. Not guaranteed everywhere. Prefer new RegExp(pattern, flags).
LegacyCompatibility only
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
compile()Avoid in new code
Bottom line: Recognize compile() in old code. For new apps, always use new RegExp(pattern, flags) or a regex literal — feature-detect if you must touch the legacy API.
Analysis
Performance Notes
Approach
Cost
Best for
regex.compile(p, f)
Recompile cost
Legacy recognition only
new RegExp(p, f)
Create + compile
Modern pattern switching
Regex literal /p/f
Compile once
Fixed patterns
Cost is similar to creating a new RegExp. The real issue is portability and surprising mutation — not microseconds. Prefer new RegExp for clarity and standards compliance.
Remember
Key Takeaways
Role: recompile an existing RegExp in place with a new pattern/flags.
Status: deprecated / non-standard — learn it, do not ship it.
Replace: use new RegExp(pattern, flags).
Mutation: same object updates — shared refs all see the change.
One line:compile() is a legacy in-place recompiler — recognize it in old code, and use new RegExp for new code.
Frequently Asked Questions
compile(pattern, flags) recompiles an existing RegExp object with a new pattern and optional flags. It mutates the same object in place and returns it.
Yes. It is a legacy, non-standard method (Netscape-era). It is not part of the ECMAScript standard. Prefer new RegExp(pattern, flags) in new code.
No. It returns the same RegExp instance after updating its pattern and flags. Any variable still pointing at that object sees the change.
Assign a fresh RegExp: regex = new RegExp(pattern, flags). That works everywhere, reads clearly, and avoids mutating a shared object.
In engines that support compile(), lastIndex is typically reset to 0 because the pattern changed. That avoids stale search positions for global or sticky regexes.
Use typeof regex.compile === "function" before calling it, then fall back to new RegExp(pattern, flags) when it is missing.