RegExp compile()
The compile() method of RegExp instances is used to recompile a regular expression with new source and flags after the RegExp object has already been created.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
RegExp.compile Deprecated | 1 | 12 | 1 | 3.1 | 18 | 2 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Live demo
Reuse one RegExp object
Call compile when available to replace the pattern on the same instance.
JavaScript
Output
Press the Run button
Switch between patterns
Retarget a single RegExp instance to check multiple formats.
JavaScript
Output
Press the Run button
Compare with new RegExp
Show the modern alternative to compile by constructing a new RegExp.
JavaScript
Output
Press the Run button
Use cases
-
Regex modernization
Replace compile()-based flows with clearer new RegExp(...) creation during refactors.
-
Legacy code reading
Recognize what compile is doing when auditing historical regular-expression utilities.
Cautions
- Mutating regex objects makes pattern flow harder to follow than building a fresh value.
- Deprecated APIs increase maintenance cost and rarely justify their continued use.
Accessibility
- No direct accessibility effect, but clearer validation code supports more reliable user feedback.
- Prefer maintainable regex handling in forms and parsing paths that drive visible errors.