JS: Regular Expression Flags
What is Regex Flag
- Regex flags change the meaning of the regular expression, or behavior of Regex Functions.
- For how to add them, see JS: Create Regex Object.
List of Regex Flags
global (g)
- Property Name:
RegExp.prototype.global - Short Code:
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)
- Property Name:
RegExp.prototype.ignoreCase - Short Code:
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)
- Property Name:
RegExp.prototype.multiline - Short Code:
m
- Make the regex syntax
^match any newline beginning. (instead of the beginning of whole string) - Make the regex syntax
$match any newline end. (instead of the end of whole string)
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)
- Property Name:
RegExp.prototype.dotAll - Short Code:
s
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)
- Property Name:
RegExp.prototype.unicode - Short Code:
u
Interpret the text to be matched as byte sequence of characters of their unicode code points.
sticky (y)
(new in ECMAScript 2015)
- Property Name:
RegExp.prototype.sticky - Short Code:
y
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
- JS: Regular Expression Tutorial
- JS: Regular Expression Functions
- JS: Create Regex Object
- JS: Regular Expression Syntax
- JS: Regular Expression Flags
- JS: Regex Replace String Dollar Sign
- JS: Regex Replace Function Args
- JS: RegExp (class)
- JS: RegExp Constructor
- JS: RegExp.prototype
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.replaceAll
- JS: RegExp.prototype.test
- JS: RegExp.prototype.exec