RegExp flags in JavaScript are modifiers that change how a regular expression behaves during pattern matching. Flags control things like case sensitivity, global matching, multi-line behavior, Unicode handling, and sticky matching. Understanding RegExp flags helps in writing accurate and flexible text search patterns.


What Are RegExp Flags in JavaScript?

RegExp flags are added after the pattern and define how the search is performed.

Syntax:

JavaScript
1/pattern/flags

Example:

JavaScript
1/hello/gi

Explanation:

  • g searches globally (finds all matches)

  • i makes the match case-insensitive


List of RegExp Flags in JavaScript

FlagNamePurpose
gGlobalFinds all matches
iCase-insensitiveIgnores letter case
mMultilineChanges ^ and $ behavior
sDotAllAllows . to match newlines
uUnicodeEnables full Unicode support
yStickyMatches from last index only

Global Flag (g)

Finds all occurrences instead of stopping at the first match.

JavaScript
1let text = "apple banana apple apple";
2let matches = text.match(/apple/g);
3
4console.log(matches);

Explanation:
Returns all occurrences of apple as an array.


Case-Insensitive Flag (i)

Ignores case while matching text.

JavaScript
1let text = "Hello hELLo HELLO";
2let matches = text.match(/hello/gi);
3
4console.log(matches);

Explanation:
Matches Hello, hELLo, and HELLO.


Multiline Flag (m)

Changes how ^ and $ work in multi-line strings.

JavaScript
1let text = `first line
2second line`;
3
4let matches = text.match(/^second/m);
5console.log(matches);

Explanation:
^second matches second at the start of the second line.


DotAll Flag (s)

Allows the dot (.) to match newline characters.

JavaScript
1let text = "Hello\nWorld";
2let result = text.match(/Hello.World/s);
3
4console.log(result);

Explanation:
With s flag, . matches the newline character between Hello and World.


Unicode Flag (u)

Enables correct handling of Unicode characters and symbols.

JavaScript
1let text = "😊";
2console.log(/😊/u.test(text));

Explanation:
Ensures proper matching of Unicode characters.


Sticky Flag (y)

Matches only from the current lastIndex position of the regex.

JavaScript
1let regex = /a/y;
2regex.lastIndex = 2;
3
4let text = "baab";
5console.log(regex.test(text));

Explanation:
Matches only if a appears exactly at index 2.


Combining Multiple Flags

Multiple flags can be used together.

JavaScript
1let text = "One one ONE";
2let matches = text.match(/one/gi);
3
4console.log(matches);

Explanation:
Finds all matches of one ignoring case.


Difference Between g and y Flags

JavaScript
1let regex = /a/y;
2regex.lastIndex = 2;
3
4let text = "baab";
5console.log(regex.test(text));

Explanation:

  • g continues searching for the next match

  • y only matches at the exact lastIndex position


Common Mistakes with RegExp Flags

  • Forgetting to use g when multiple matches are required

  • Using ^ and $ without m in multi-line strings

  • Expecting . to match new lines without s

  • Misunderstanding the behavior of the y flag


Summary of RegExp Flags

FlagBehavior Change
gFinds all matches
iIgnores case
mMultiline anchors
sDot matches newline
uUnicode support
ySticky position-based matching

Conclusion

RegExp flags in JavaScript modify how patterns match text and make regular expressions more flexible and powerful. By using flags like g, i, m, s, u, and y, text searching, validation, and parsing tasks become easier and more precise.