new RegExp(pattern, flags?) creates a RegExp object from a pattern string and optional flags. Use it when the pattern is built at runtime; use a literal for fixed patterns.
Remember
new RegExp(pattern, flags?)
new RegExp("cat", "i") → /cat/i
new RegExp("\\d+") → /\d+/
literal (fixed): /cat/i
constructor (dynamic): new RegExp(word, "gi")
regex.constructor === RegExp → true
Use
Prefer
Fixed pattern
/pattern/flags literal
Dynamic pattern / flags
new RegExp(pattern, flags)
Type check
value instanceof RegExp
Approach
How to Use new RegExp()
Pass a pattern string, optional flags, then use test / exec.
Build the pattern as a string (escape metacharacters if it comes from user input).
Call new RegExp(pattern, flags) — flags like "g", "i", "m".
Use the result with test(), exec(), or string methods like match.
For a constant pattern, prefer a literal /pattern/flags instead.
Piece
Required?
Role
pattern
Yes
Pattern string (or another RegExp)
flags
Optional
Flag string, e.g. "gi"
Return value
—
New RegExp object
Pseudocode
Pseudocode
pattern = string pattern
flags = optional flags string
if pattern is invalid:
throw SyntaxError
return new RegExp object with that pattern and flags
// same kind of object as /pattern/flags
Reference
Cheat Sheet
Keep these facts handy while creating regexes.
Topic
Detail
Syntax
new RegExp(pattern, flags?)
Common flags
g global, i ignore case, m multiline
vs literal
Constructor for dynamic patterns; literal for fixed
Backslashes
In strings, write "\\d" to get \d
.constructor
Instance property pointing at RegExp
Invalid pattern
Throws SyntaxError
Standard
Core language — all environments
Try it
Live Preview
Build a RegExp with the constructor and test it against sample text.
Browser demo of new RegExp(pattern, flags) — same idea as the examples below.
Literal form./\d+/ is a fixed digit pattern written in code.
Constructor form. In a string you need "\\d+" so the regex engine sees \d+.
Same tests. Both match "42".
Same type. Both pass instanceof RegExp.
Edge Cases & Pitfalls
Check these before building regexes from strings.
trap
Double backslashes in strings
Write "\\d" in a string to get the pattern \d. A single "\d" is not a digit class.
syntax
Invalid patterns throw
new RegExp("(") throws SyntaxError. Guard dynamic patterns with try/catch.
security
Escape user input
Unescaped metacharacters in user text can change meaning or break the pattern. Escape before interpolating.
typecheck
Prefer instanceof RegExp
constructor can be overwritten. instanceof is the usual type check for RegExp values.
Compatibility
Browser & Runtime Support
new RegExp(pattern, flags) is a core language feature — available in every JavaScript environment.
✓ Baseline · Widely available
RegExp constructor
Supported in all browsers and Node.js versions. Safe for every target with no polyfill.
100%Universal support
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
new RegExp()Universal
Bottom line: Use freely. Prefer literals for fixed patterns and the constructor when the pattern or flags are dynamic.
Analysis
Performance Notes
Approach
Cost
Best for
Literal /p/f
Compile once
Fixed patterns
new RegExp(p, f)
Compile each call
Dynamic patterns
Reuse one RegExp
Cheapest in loops
Repeated matching
Creating a RegExp has a compile cost. Build it once outside a hot loop when the pattern does not change. Literals are fine and often clearer for constants.
Remember
Key Takeaways
Role: create a RegExp from a pattern string and optional flags.
When: use the constructor for dynamic patterns; literals for fixed ones.
Strings: remember "\\d" in code to get the pattern \d.
Type check: prefer instanceof RegExp over trusting .constructor.
One line:new RegExp(pattern, flags) builds regexes from strings — use it when the pattern is dynamic; use literals when it is fixed.
Frequently Asked Questions
new RegExp(pattern, flags) creates a RegExp object from a pattern string and optional flags. Use it when the pattern or flags are built at runtime.
Use a literal (/pattern/flags) for fixed patterns. Use new RegExp when the pattern or flags come from variables, user input, or string concatenation.
Yes for clarity: new RegExp(pattern, flags). Calling RegExp(pattern, flags) without new also creates a RegExp, but new is the usual style.
Every RegExp instance has a constructor property that points to the RegExp function. Prefer instanceof RegExp for type checks; use new RegExp(...) to create regexes.
Escape regex metacharacters in user text before building the pattern — for example replace special chars with a backslash-escaped form — or you may get a SyntaxError or unintended matches.
new RegExp throws a SyntaxError if the pattern string is not valid. Wrap creation in try/catch when the pattern is untrusted or built dynamically.