JS: Regular Expression Flags

By Xah Lee. Date: . Last updated: .

What is Regex Flag

List of Regex Flags

global (g)

Find all matches.

Used by many regex functions. 〔see JS: Regular Expression Functions

// demo diff of regex flag g

/* get all occurrence of a match */
console.log("something".match(/[aeiou]/g));
// [ "o", "e", "i" ]

console.log("something".match(/[aeiou]/));
// [ "o", index: 1, input: "something", groups: undefined ]
// RegExp.prototype.global
const xx = /([aeiou])/g;
console.log(xx.global);
// true

ignoreCase (i)

Ignore case.

// demo diff of ignoreCase

console.log(/a/i.test("ABC") === true);

console.log(/a/.test("ABC") === false);
const xx = /a/i;
console.log(xx.ignoreCase);
// true

multiline (m)

console.log(/^B/.test("A\nB") === false);
console.log(/^B/m.test("A\nB") === true);
console.log(/a/m.multiline);
// true

dotAll (s)

(new in ECMAScript 2018)

Make the dot . also match newline characters. 〔see RegExp Syntax

console.log(/A.B/.test("A\nB") === false);
console.log(/A.B/s.test("A\nB") === true);

unicode (u)

(new in ECMAScript 2015)

Interpret the text to be matched as byte sequence of characters of their unicode code points.

sticky (y)

(new in ECMAScript 2015)

Make the match start at the index RegExp.prototype.lastIndex. (does not change the meaning of ^. It still mean beginning of string or line.)

JavaScript. Regular Expression