Last Updated : 05 Aug, 2025
RegExp flags are a set of optional characters added to the regular expression pattern to control its behavior. Flags modify how the pattern is interpreted during the search. They can be added after the closing / of the regular expression pattern.
JavaScript
let s = "Hello World\nhello world";
// Global search flag (`g`)
let regexG = /hello/g;
console.log(s.match(regexG));
// Case-insensitive search flag (`i`)
let regexI = /hello/i;
console.log(s.match(regexI));
// Multi-line search flag (`m`)
let regexM = /^hello/m;
console.log(s.match(regexM));
// Dot-all flag (`s`)
let regexS = /hello.world/s;
console.log(regexS.test(s));
// Unicode flag (`u`)
let regexU = /\u{1F600}/u;
console.log(regexU.test("π"));
// Sticky flag (`y`)
let regexY = /hello/y;
console.log(regexY.exec(s));
[ 'hello' ] [ 'Hello', index: 0, input: 'Hello World\nhello world', groups: undefined ] [ 'hello', index: 12, input: 'Hello World\nhello world', groups: undefined ] true true nullThe most commonly used flags are
The g flag tells the regular expression to match all occurrences of the pattern in the string.
JavaScript
let regex = /apple/g;
let s = "apple apple apple";
console.log(s.match(regex));
[ 'apple', 'apple', 'apple' ]2. Case-Insensitive Flag (i)
The i flag makes the regular expression case-insensitive, allowing it to match patterns regardless of letter case.
JavaScript
let regex = /hello/i;
let s = "Hello world";
console.log(regex.test(s));
3. Multi-Line Flag (m)
The m flag treats the start (^) and end ($) anchors as matching the beginning and end of each line in a multi-line string.
JavaScript
let regex = /^world/m;
let s = "hello\nworld";
console.log(regex.test(s));
4. Dot-All Flag (s)
The s flag enables the dot (.) to match newline characters as well, which is not its default behavior.
JavaScript
let regex = /hello.world/s;
let s = "hello\nworld";
console.log(regex.test(s));
5. Unicode Flag (u)
The u flag is used for full Unicode matching, allowing you to match characters outside the Basic Multilingual Plane (BMP) such as emojis and special symbols.
JavaScript
let regex = /\u{1F600}/u; // Unicode emoji: π
let s = "π";
console.log(regex.test(s));
6. Sticky Flag (y)
The y flag ensures that the regular expression matches only at the exact position where the last match ended, avoiding any overlap.
JavaScript
let regex = /apple/y;
let s = "apple apple apple";
console.log(regex.exec(s));
console.log(regex.exec(s));
[ 'apple', index: 0, input: 'apple apple apple', groups: undefined ] nullWhen to Use RegExp Flags?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4