bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript RegExp Flags

RegExp Modifier Flags

Flags are parameters that can modify how a regex pattern is used, such as making it case-insensitive or global.

/pattern/ flags

JavaScript Regex Flags

Revised July 2025

Flag

Description

/d Performs substring matches (new 2022) /g Performs a global match (find all) /i

Performs case-insensitive matching

/m

Performs multiline matching

/s Allows . (dot) to match line terminators (new 2018) /u Enables Unicode support (new 2015) /v

Formula

An upgrade to the /u flag for better Unicode support (new 2025)

/y Performs a "sticky" search (new 2015)

Formula

Flag Syntax: /pattern/flags

/

Opening delimiter for the regular expression pattern

Regular expression (a search criteria) /

Closing delimiter for the regular expression flags

One or more single modifier flags

Formula

RegExp /g Flag (Global)

The

/g flag matches all occurrences of the pattern, rather than just the first one.

Example

A global search for "is" in a string:
let text = "Is this all there is?";
const pattern = /is/g;
let result = text.match(pattern);

Formula

RegExp /i Flag (Insensitive)

The

Formula

/i flag makes the match case - insensitive: /abc/i matches "abc", "AbC", "ABC".

Example

Formula

A case - insensitive search for "w3schools" in a string:
let text = "Visit W3Schools";
const pattern = /is/g;
let result = text.match(pattern);

Formula

RegExp /d Flag

The

/d flag specifies the start and the end of a match.

Example

Match every sub text that starts or ends with aa or bb:

let text = "aaaabb";
const pattern = /(aa)(bb)/d;
let result = text.match(pattern);

Formula

RegExp /s Flag (Single line/DotAll)

The

/s flag allows the . (dot) metacharacter to match newline characters (\n) in addition to any other character.

Example

Formula

Without the /s flag, \n does not match . (wildchars):
let text = "Line\nLine.";
const pattern = /Line./gs;
let result = text.match(pattern);

RegExp /y Flag (Sticky):

The

/y flag performs a "sticky" search from the lastIndex property of the RegExp object.

The

/y flag lets a match start at the exact position where the last match ended.

Examples let text = "abc def ghi";
const pattern = /\w+/y;
// Start match from position 4 pattern.lastIndex = 4;
let result = text.match(pattern);

Formula

The /y flag must be set to allow match from a position.

This will not work:

let text = "abc def ghi";
const pattern = /\w+/;
// Start match from position 4 pattern.lastIndex = 4;
let result = text.match(pattern);

The example above uses a regex metacharacter

/\w+/.

The meaning of

/\w+/ is "mach any word". You will learn more about metacharacters in the next chapters.

Formula

RegExp /u Flag (Unicode)

Previous

JavaScript Math Reference

Next

JavaScript String Methods