RegExp
A regular expression: a pattern for searching, validating, and replacing pieces of text.
- Returns
- A new
RegExpobject.
Example
#const postalCode = /^\d{2}-\d{3}$/
console.log(postalCode.test('00-950'))
console.log(postalCode.test('00950'))
console.log(postalCode)^ 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
#/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
#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)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'))const hasDigit = /\d/g
console.log(hasDigit.test('a1'))
console.log(hasDigit.test('a1'))
console.log(hasDigit.test('a1'))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 fornew RegExp()you have to double the backslash:"\\d". - Don’t use the
gflag withtest()when you call it repeatedly on the same expression. The next call might returnfalse. - Escape special characters (e.g.
.,*,?) in user-provided text before inserting it into a pattern. Newer browsers have theRegExp.escape()method for this.
Related entries
#- String.prototype.replace()Returns a new string in which the first match of a pattern (or all matches, with the g flag) has been replaced with other text.
- String.prototype.match()Matches a string against a regular expression. Returns an array with the match or all matches, or null.
- String.prototype.split()Splits a string into an array of pieces based on a separator: a character, a string, or a regular expression.
- String.prototype.replaceAll()Returns a new string in which every occurrence of a pattern has been replaced with the given text or the result of a function.
- String.prototype.includes()Checks whether a string contains the given piece of text. Returns true or false, and is case-sensitive.
- String.prototype.search()Returns the index of the first match of a regular expression in a string, or -1.
Spotted a mistake or missing an example? Write to us.