Table of Contents
Flags modify how a regular expression searches for patterns. The syntax is /pattern/flags:
| Flag | Description |
|---|---|
/g |
Global - matches all occurrences |
/i |
Insensitive - case insensitive |
/m |
Multiline - ^ and $ match line breaks |
/s |
DotAll - . matches newlines too |
/y |
Sticky - matches only from lastIndex |
/u |
Unicode - proper Unicode handling |
The /g flag searches for all occurrences, not just the first:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Flags</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp /g Flag (Global)</h4>
<p id="demo"></p>
<script>
const text = "apple banana apple cherry apple";
// Without /g - only first match
const first = text.match(/apple/);
// With /g - all matches
const all = text.match(/apple/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"without /g: " + first + "<br>" +
"with /g: " + all + " (all occurrences)";
</script>
</body>
</html>

Result View Example
The /i flag makes the search case insensitive:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Flags</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp /i Flag (Insensitive)</h4>
<p id="demo"></p>
<script>
const text = "JavaScript is FUN!";
// Case sensitive
const sensitive = text.match(/fun/);
// Case insensitive
const insensitive = text.match(/fun/i);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/fun/): " + sensitive + " (no match)<br>" +
"match(/fun/i): " + insensitive + " (matched)";
</script>
</body>
</html>

Result View Example
The /m flag makes ^ and $ match the beginning/end of each line:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Flags</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp /m Flag (Multiline)</h4>
<p id="demo"></p>
<script>
const text = "line1\nline2\nline3";
// Without /m - ^ only matches start of string
const withoutM = text.match(/^line/g);
// With /m - ^ matches start of each line
const withM = text.match(/^line/gm);
document.getElementById("demo").innerHTML =
"Text:<br>" + text + "<br><br>" +
"match(/^line/g): " + withoutM + "<br>" +
"match(/^line/gm): " + withM + " (each line)";
</script>
</body>
</html>

Result View Example
The /y flag makes the search sticky - it only matches starting at lastIndex:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Flags</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp /y Flag (Sticky)</h4>
<p id="demo"></p>
<script>
const text = "abcabc";
const sticky = /abc/y;
// First match at position 0
const first = sticky.exec(text);
// After lastIndex = 3, matches "abc" again at position 3
const second = sticky.exec(text);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"sticky = /abc/y<br>" +
"First exec: " + first[0] + " at index " + first.index + "<br>" +
"lastIndex after: " + sticky.lastIndex + "<br>" +
"Second exec: " + second[0] + " at index " + second.index;
</script>
</body>
</html>

Result View Example
The /u flag enables proper Unicode handling:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Flags</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp /u Flag (Unicode)</h4>
<p id="demo"></p>
<script>
const text = "Café 100";
// \d finds ASCII digits only
const asciiDigits = text.match(/\d/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/\\d/g): " + asciiDigits + "<br><br>" +
"<b>Other flag properties:</b><br>" +
"global: " + /a/g.global + "<br>" +
"ignoreCase: " + /a/i.ignoreCase + "<br>" +
"multiline: " + /a/m.multiline + "<br>" +
"sticky: " + /a/y.sticky + "<br>" +
"unicode: " + /a/u.unicode;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.