Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท RegExp

The RegExp object and constructor

Reference for the JavaScript RegExp constructor and literals: build patterns from strings, copy flags, escape user input and handle invalid patterns.

A RegExp object holds a regular expression: a pattern that describes text, plus flags that change how it is matched. You can create one with a literal such as /\d+/g, which is compiled when the script loads, or with the constructor new RegExp("\\d+", "g"), which builds the pattern from strings at run time.

Use a literal when the pattern is fixed. Use the constructor when part of the pattern comes from a variable, such as a search term the user typed.

JavaScript
const literal = /colou?r/i;
const built = new RegExp("colou?r", "i");
literal.test("Color"); // true

Try it

JS Literal or constructor

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Patterns built from user input

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
/pattern/flags
new RegExp(pattern)
new RegExp(pattern, flags)
RegExp(pattern, flags)
Name Description
pattern A string with the pattern text, or another RegExp whose pattern is copied.
flags A string of flag letters from d, g, i, m, s, u, v and y. If given with a RegExp pattern, it replaces that regex's flags.

Return value: a new RegExp object. Calling RegExp() without new returns the argument itself when it is already a regex (and no flags are passed); otherwise it also creates a new one.

Exceptions: a SyntaxError is thrown if the pattern is invalid or the flags contain an unknown or repeated letter, or both u and v.

Usage notes

  • Double the backslashes in strings. "\\d" produces the pattern \d, because "\d" is just "d". Prefer a literal whenever the pattern is fixed.
  • Escape user input before putting it in a pattern; special characters such as . * + ? ( ) [ ] would otherwise change the meaning. Newer engines offer RegExp.escape() for this; see its own page.
  • Literals in loops create a new object each time the expression is evaluated, which matters for stateful g and y regexes, because each object has its own lastIndex.
  • Properties: source, flags and one boolean per flag (global, ignoreCase, multiline, dotAll, unicode, unicodeSets, sticky, hasIndices) are read-only. lastIndex is the one writable property.
  • Flags are normalized. new RegExp("a", "ig").flags is "gi", always in a fixed order.
  • / in a string pattern is written escaped in source, so the text of the regex stays valid as a literal.
  • An empty pattern becomes (?:), because // would be a comment.
  • Where regexes are used: test(), exec(), and the string methods match(), matchAll(), replace(), replaceAll(), search() and split().
  • Performance: compile once and reuse the object. Avoid patterns with nested open-ended repetition such as (a+)+, which can take exponential time on bad input.

Browser support

RegExp literals and the constructor work everywhere. Newer features inside patterns (the v flag, the d flag, lookbehind, named groups) have varying support in older browsers, so check a compatibility table.