JavaScript RegExp Flags

Modifying pattern matching behavior

🚩 What are RegExp Flags?

Flags are optional parameters that modify how a regular expression pattern is interpreted. They control case sensitivity, global matching, multiline behavior, and more.


// Without flag (case sensitive)
let pattern1 = /hello/;
console.log(pattern1.test("Hello")); // false

// With 'i' flag (case insensitive)
let pattern2 = /hello/i;
console.log(pattern2.test("Hello")); // true
                                    

Output:

false
true

Common RegExp Flags

🔤

i - Case Insensitive

Ignores case when matching

let pattern = /cat/i;
console.log(pattern.test("CAT")); // true
console.log(pattern.test("Cat")); // true
🌐

g - Global

Finds all matches, not just first

let text = "cat bat cat";
let matches = text.match(/cat/g);
console.log(matches); // ["cat", "cat"]
📝

m - Multiline

^ and $ match line breaks

let text = "line1\nline2";
let pattern = /^line/gm;
console.log(text.match(pattern)); // ["line", "line"]
🔍

s - Dotall

. matches newline characters

let text = "hello\nworld";
let pattern = /hello.world/s;
console.log(pattern.test(text)); // true

🔹 The 'i' Flag - Case Insensitive

Makes pattern matching ignore case differences:

// Without 'i' flag
let pattern1 = /javascript/;
console.log(pattern1.test("JavaScript")); // false
console.log(pattern1.test("javascript")); // true

// With 'i' flag
let pattern2 = /javascript/i;
console.log(pattern2.test("JavaScript")); // true
console.log(pattern2.test("JAVASCRIPT")); // true
console.log(pattern2.test("javascript")); // true

// Practical example: case-insensitive search
function searchText(text, searchTerm) {
    let pattern = new RegExp(searchTerm, 'i');
    return pattern.test(text);
}

console.log(searchText("Hello World", "hello")); // true

Output:

false
true
true
true
true
true

🔹 The 'g' Flag - Global Matching

Finds all matches instead of stopping at the first one:

let text = "The cat in the hat sat on the mat";

// Without 'g' flag - finds only first match
let pattern1 = /at/;
console.log(text.match(pattern1)); // ["at"]

// With 'g' flag - finds all matches
let pattern2 = /at/g;
console.log(text.match(pattern2)); // ["at", "at", "at", "at"]

// Replace all occurrences
let newText = text.replace(/at/g, "og");
console.log(newText); // "The cog in the hog sog on the mog"

// Count matches
let matches = text.match(/the/gi); // 'g' + 'i' flags combined
console.log(matches.length); // 3

Output:

["at"]
["at", "at", "at", "at"]
"The cog in the hog sog on the mog"
3

🔹 The 'm' Flag - Multiline

Changes behavior of ^ and $ to match line boundaries:

let multilineText = `First line
Second line
Third line`;

// Without 'm' flag - ^ matches only start of string
let pattern1 = /^Second/;
console.log(pattern1.test(multilineText)); // false

// With 'm' flag - ^ matches start of any line
let pattern2 = /^Second/m;
console.log(pattern2.test(multilineText)); // true

// Find all lines starting with a word
let pattern3 = /^\w+/gm;
let lineStarts = multilineText.match(pattern3);
console.log(lineStarts); // ["First", "Second", "Third"]

// Match lines ending with "line"
let pattern4 = /line$/gm;
let lineEnds = multilineText.match(pattern4);
console.log(lineEnds); // ["line", "line", "line"]

Output:

false
true
["First", "Second", "Third"]
["line", "line", "line"]

🔹 Combining Multiple Flags

You can use multiple flags together:

let text = `Hello World
HELLO UNIVERSE
hello everyone`;

// Combine 'g', 'i', and 'm' flags
let pattern = /^hello/gim;
let matches = text.match(pattern);
console.log(matches); // ["Hello", "HELLO", "hello"]

// Practical example: case-insensitive global replace
let emailText = "Contact: [email protected] or [email protected]";
let cleanEmails = emailText.replace(/[A-Z]/g, function(match) {
    return match.toLowerCase();
});
console.log(cleanEmails); // "contact: [email protected] or [email protected]"

// Extract all email addresses (case insensitive)
let emailPattern = /\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\b/gi;
let emails = emailText.match(emailPattern);
console.log(emails); // ["[email protected]", "[email protected]"]

Output:

["Hello", "HELLO", "hello"]
"contact: [email protected] or [email protected]"
["[email protected]", "[email protected]"]

🔹 Flag Properties

You can check which flags a RegExp object has:

let pattern = /hello/gim;

// Check individual flags
console.log(pattern.global);     // true
console.log(pattern.ignoreCase); // true
console.log(pattern.multiline);  // true

// Get all flags as string
console.log(pattern.flags);      // "gim"

// Get the source pattern
console.log(pattern.source);     // "hello"

// Create new RegExp with same pattern but different flags
let newPattern = new RegExp(pattern.source, 'gi');
console.log(newPattern.flags);   // "gi"

Output:

true
true
true
"gim"
"hello"
"gi"

🧠 Test Your Knowledge

Which flag makes a RegExp case insensitive?