JavaScript Notes
Master all JavaScript regex flags: g (global), i (case-insensitive), m (multiline), s (dotAll), u (unicode), y (sticky). With visual examples, common mistakes, and interview Q&A.
Flags modify how a regex pattern behaves. They come after the closing / delimiter in a literal regex (/pattern/flags) or as the second argument to RegExp(). You can combine multiple flags.
📊 All Regex Flags at a Glance
💻 Example 1 — g Flag (Global)
First: JavaScript All: [ 'JavaScript', 'javascript' ] JS is great. JS runs everywhere. JS is fun. JS is great. JS runs everywhere. javascript is fun.
💻 Example 2 — i Flag (Case-Insensitive)
false false true true true true [ 'JavaScript', 'java', 'TypeScript', 'JAVA' ]
💻 Example 3 — m Flag (Multiline)
const multiline = `Line 1: First line
Line 2: Second line
Line 3: Third line`;
// Without m — ^ matches only start of WHOLE string, $ matches end of WHOLE string
const withoutM = multiline.match(/^Line \d:/g);
console.log("Without m:", withoutM);
// With m — ^ matches start of EACH line, $ matches end of EACH line
const withM = multiline.match(/^Line \d:/gm);
console.log("With m:", withM);
// Extract only lines ending with "line"
const endsLine = multiline.match(/.+line$/gim);
console.log("Lines ending with 'line':", endsLine);Without m: [ 'Line 1:' ] With m: [ 'Line 1:', 'Line 2:', 'Line 3:' ] Lines ending with 'line': [ 'Line 1: First line', 'Line 2: Second line', 'Line 3: Third line' ]
💻 Example 4 — s Flag (dotAll — dot matches newlines)
Without s: null With s: <div> Hello World </div>... false true
💻 Example 5 — u Flag (Unicode) and y Flag (Sticky)
null [ '🌍', '🚀' ] [ '1', '2', '3', index: 0, input: '123abc456', groups: undefined ] 3 null [ '4', '5', '6', index: 6, input: '123abc456', groups: undefined ]
📋 Flag Combinations in Real Use
⚠️ Common Mistakes
❌ Mistake 1 — Using g Flag with test() in a Loop (stateful lastIndex)
true false true
Thegflag makes regex stateful —lastIndexadvances after eachtest(). Alternate calls give inconsistent results. Fix: create a new regex inside the loop, or use/pattern/.test(str)(fresh literal each time).
❌ Mistake 2 — Forgetting m Flag for Multiline Line Start/End
null [ 'ERROR: disk full' ]
❌ Mistake 3 — Using s Flag Without Checking Browser Support
Thes(dotAll) flag was introduced in ES2018. For older environments (Node < 10, older browsers), use[\s\S]as a workaround for matching newlines:/[\s\S]*/instead of/.*/s.
🎯 Key Takeaways
g— find all matches, not just the first; also makes regex stateful (updateslastIndex)i— case-insensitive matchingm—^and$match start/end of each line instead of the whole strings— dotAll:.matches newlines too (ES2018+)u— full Unicode support; required for\p{...}Unicode property escapesy— sticky: matches only fromlastIndexposition- Flags can be combined:
gi,gm,gis, etc.
❓ Interview Questions
Q1. What does the g flag do in JavaScript regex? > The g (global) flag makes the pattern find all matches in a string instead of stopping at the first. Methods like match() with g return an array of all matches. It also makes the regex stateful — lastIndex advances after each match with exec() or test().
Q2. What is the difference between /pattern/i and /pattern/? > /pattern/i is case-insensitive — a matches both a and A. Without i, matching is case-sensitive by default.
Q3. When do you need the m flag? > When working with multiline strings and you want ^ and $ to match the start and end of each individual line, not just the beginning and end of the entire string.
Q4. What problem can the g flag cause with test() in a loop? > A regex with g is stateful — it updates lastIndex after each test(). In a loop, consecutive test() calls alternate between true and false as lastIndex advances. Fix: use a fresh regex literal inside the loop, or avoid g with test().
Q5. What does the s (dotAll) flag do? > The s flag makes the dot . match any character including newline (\n, \r). By default, . does NOT match newlines.
Q6. What is the u flag used for? > The u flag enables full Unicode mode, allowing correct handling of characters outside the Basic Multilingual Plane (emoji, some Asian scripts). It is required for Unicode property escapes (\p{Letter}, \p{Emoji}).
Q7. Can you use multiple flags together? > Yes — combine them freely after the closing delimiter: /pattern/gi (global + case-insensitive), /pattern/gm (global + multiline), /pattern/gis (global + case-insensitive + dotAll).
Q8. What is the y (sticky) flag? > The sticky flag forces the regex to match starting exactly at lastIndex — it won't skip ahead to find a match. Useful for tokenizers that process a string sequentially without backtracking.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Regex Flags – g, i, m, s, u, y Flags Explained with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, regex, flags, javascript regex flags – g, i, m, s, u, y flags explained with examples
Related JavaScript Master Course Topics