RegExp

A regular expression: a pattern for searching, validating, and replacing pieces of text.

Returns
A new RegExp object.
On this page

Example

#
JavaScript
const postalCode = /^\d{2}-\d{3}$/

console.log(postalCode.test('00-950'))
console.log(postalCode.test('00950'))
console.log(postalCode)
Console
true
false
/^\d{2}-\d{3}$/

^ and $ mark the start and end of the text, \d matches any digit, and {2} means exactly two repetitions.

Definition and usage

#

A regular expression describes a text pattern, for example "all digits," "a postal code," or "a word starting with a capital letter." You create one as a literal between slashes, /pattern/flags, or with the new RegExp("pattern", "flags") constructor when the pattern is built dynamically, e.g. from text typed by the user.

The test() method checks whether a text matches the pattern and returns true or false. Regular expressions also work with string methods: match() and matchAll() find matches, replace() and replaceAll() swap out fragments, and split() divides the text.

Flags change how the pattern behaves: g finds all matches, i ignores letter case, m makes ^ and $ work for every line, and u turns on full Unicode support. Parentheses ( ) create groups, which let you read out parts of the match, and (?<name>...) gives a group a name.

Syntax

#
Syntax
/pattern/flags
new RegExp(pattern)
new RegExp(pattern, flags)

regex.test(text)
text.match(regex)
text.replace(regex, replacement)

Parameters

#
  • pattern

    string or RegExp

    The pattern. In the constructor it’s a plain string, so backslashes must be doubled: "\\d+".
  • flags

    optional string

    Flags, e.g. "g", "i", or "gi".

More examples

#
All matches and case sensitivity
JavaScript
const text = 'Task 3 for 15 pts, task 7 for 40 pts'

console.log(text.match(/\d+/g))
console.log(text.match(/task/gi).length)
console.log(text.match(/task/g).length)
Console
['3', '15', '7', '40']
2
1
Groups and replacement
JavaScript
const date = '2026-09-28'
const { groups } = date.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)

console.log(groups.day, groups.month, groups.year)
console.log(date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3.$2.$1'))
Console
28 09 2026
28.09.2026
The pitfall: the g flag and test()
JavaScript
const hasDigit = /\d/g

console.log(hasDigit.test('a1'))
console.log(hasDigit.test('a1'))
console.log(hasDigit.test('a1'))
Console
true
false
true

With the g flag, the expression remembers in lastIndex where it stopped searching, and the next call starts from there. For a simple check, skip the g flag.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • In a /.../ literal you write \d, but in a string for new RegExp() you have to double the backslash: "\\d".
  • Don’t use the g flag with test() when you call it repeatedly on the same expression. The next call might return false.
  • Escape special characters (e.g. ., *, ?) in user-provided text before inserting it into a pattern. Newer browsers have the RegExp.escape() method for this.

Related entries

#

Spotted a mistake or missing an example? Write to us.