Open In App

JavaScript RegExp Flags Property

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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));

Output
[ 'hello' ]
[
  'Hello',
  index: 0,
  input: 'Hello World\nhello world',
  groups: undefined
]
[
  'hello',
  index: 12,
  input: 'Hello World\nhello world',
  groups: undefined
]
true
true
null

The most commonly used flags are

  • g: Global search: matches all occurrences of the pattern in the string.
  • i: Case-insensitive search: ignores case during matching.
  • m: Multi-line search: treats the start and end anchors (^ and $) as matching the start and end of each line within the string.
  • s: Dot-all: allows the dot (.) to match newline characters.
  • u: Unicode: enables full Unicode matching, supporting characters outside the BMP (Basic Multilingual Plane).
  • y: Sticky: matches only at the exact position where the last match ended.

Main RegExp Flags and Their Usage

1. Global Search Flag (g)

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));

Output
[ '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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
[ 'apple', index: 0, input: 'apple apple apple', groups: undefined ]
null

When to Use RegExp Flags?

  • g (Global Search): Use this flag when you want to find all occurrences of a pattern in a string rather than just the first match.
  • i (Case-Insensitive Search): This is useful when you want to match text regardless of whether it’s uppercase or lowercase.
  • m (Multi-Line Search): Useful when you need to match the start or end of each line, especially in text files or logs.
  • s (Dot-All): If you need to match patterns that span across multiple lines, such as when dealing with multiline strings.
  • u (Unicode): This flag is important when working with emojis or characters beyond the BMP in Unicode.
  • y (Sticky): Best used when you want to ensure the match starts from the exact position where the last one ended, especially in tokenization or parsing tasks.

Recommended Links:



Next Article

Similar Reads