Favorites

JavaScript RegExp Object

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
RegExp
JavaScript Text · ConstructorThis object is supported by all modern browsers.
Updated 28th November 2026

A RegExp is a regular expression: a pattern used to search, match and replace text. Patterns are written between slashes, such as /\d+/g, and used with methods like test(), match() and replace().

Description

A RegExp object holds a regular expression, a pattern for matching text. Write one as a literal between slashes, /colou?r/i, or build one from a string with new RegExp() when the pattern isn't known in advance (remember to escape backslashes in the string). Flags after the pattern change how it works: g finds all matches, i ignores case, m makes ^ and $ match at line breaks, s lets . match newlines, u and v enable Unicode features, y is sticky and d adds match indices.

Regular expressions are used with RegExp methods such as test() and exec(), and with string methods such as match(), matchAll(), replace(), search() and split(). A regex with the g or y flag remembers its position in lastIndex, which can cause surprising results if you reuse it with test().

Capture groups, written with parentheses, let you pull parts out of a match, and named groups, (?<year>\d{4}), make the result easier to read through the groups property. Lookaheads and lookbehinds, such as (?=...) and (?<=...), check what comes before or after a position without including it in the match. Regular expressions can be hard to read, so keep them short, add comments nearby, and test them with sample input.

Creating

/pattern/flags
new RegExp(pattern, flags)

Instance properties

NameDescription
RegExp.prototype.lastIndexThe lastIndex property holds the position where a global or sticky regular expression will start its next search.

Instance methods

NameDescription
RegExp.prototype.exec()The exec() method searches a string for a regular expression match and returns details of the match, including capture groups.
RegExp.prototype.test()The test() method checks whether a regular expression matches a string, returning true or false.

Example

const re = /(\d{4})-(\d{2})-(\d{2})/;
const match = '2026-11-28'.match(re);
console.log(match[1], match[2], match[3]);

console.log(/cat/i.test('Concatenate'));
console.log('a1b22c333'.match(/\d+/g));
console.log('hello world'.replace(/o/g, '0'));

const word = 'world';
console.log(new RegExp(word).test('hello world'));

Browser Support

The following information will show you the current browser support for the RegExp object. Hover over a browser icon to see the version that first introduced support for this object.

This object is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee