Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

RegExp — regular expressions

RegExp describes patterns for searching text. Learn flags, groups, character classes, quantifiers and exec() result fields, including named captures, index ranges and stateful lastIndex behavior.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

/pattern/flags; new RegExp("pattern", "flags"); regexp.exec(text); regexp.test(text)

RegExp represents a text pattern. Create one with /pattern/flags or new RegExp("pattern", "flags"); constructor strings need an extra level of backslash escaping: /\d+/ corresponds to new RegExp("\\d+"). test() returns a boolean; exec() returns a match array or null. This entry covers RegExp flags, groups, classes, quantifiers and result fields.

Flags: g enables successive matching, i ignores case, m makes ^ and $ recognize line boundaries, s lets . match line terminators, u enables Unicode mode, v extends Unicode mode with character-set operations, y requires a match exactly at lastIndex, and d adds index ranges. u and v are mutually exclusive; duplicate or unknown flags cause SyntaxError.

Classes such as [a-z], [^0-9] and \d select characters; \d means ASCII digits. Groups (…) capture text, (?<name>…) names a capture, and (?:…) does not capture. Quantifiers *, +, ? and {n,m} mean zero or more, one or more, zero or one, and between n and m repetitions. They are greedy by default; a trailing ? makes them lazy.

An exec() result contains the full match at [0], captures at [1] onward, the starting position in index, the original text in input, and named captures in groups, or undefined without named groups. Unmatched captures are undefined. Only with the d flag does the match array include an indices array: indices[0] is the pair for the full match, and the following entries are pairs for the corresponding captures or undefined for captures that did not participate. Each pair gives a [start,end) range (end exclusive) measured in UTF-16 code units, as are index and lastIndex. If the pattern has no named captures, indices.groups is undefined. Otherwise, it is an object that maps each capture name to its index pair or to undefined if that capture did not participate.

With g or y, both exec() and test() change lastIndex: success sets the match end; failure resets it to zero. An empty match does not automatically advance it in exec(), so a loop can fail to terminate. Named groups are ES2018, d is ES2022, and v is ES2024. These are ECMAScript features, not Web APIs; check target-browser support independently of Node 24 in the repository.

Examples

const re = /(?<key>[a-z]+)=(\d{2})/dg;
const text = "id=42";
const m = re.exec(text);
if (m === null) throw new Error("Expected a match");
console.log(JSON.stringify({
  match: m[0],
  captures: [m[1], m[2]],
  key: m.groups.key,
  index: m.index,
  input: m.input,
  span: m.indices[0],
  keySpan: m.indices.groups.key,
  lastIndex: re.lastIndex
}));

Result

{"match":"id=42","captures":["id","42"],"key":"id","index":0,"input":"id=42","span":[0,5],"keySpan":[0,2],"lastIndex":5}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources