Last Updated : 11 Jul, 2025
The ignoreCase property in JavaScript regular expressions determines whether the i (ignore case) flag is enabled. A regular expression with the i flag performs a case-insensitive match, meaning it ignores differences between uppercase and lowercase characters during the matching process.
JavaScript
// Regular expression without 'i' flag
let regex1 = /test/;
console.log(regex1.ignoreCase);
// Regular expression with 'i' flag
let regex2 = /test/i;
console.log(regex2.ignoreCase);
regex.ignoreCaseKey Points
Here, the i flag allows the regex to match "JavaScript" regardless of its case in the input string.
JavaScript
let s = "JavaScript is fun";
let regex = /javascript/i;
console.log(regex.test(s));
2. Searching for Case-Insensitive Substrings
The regex matches "JS" in the string despite being uppercase.
JavaScript
let s = "Learn Programming with JS";
let regex = /js/i;
console.log(regex.test(s));
3. Case-Insensitive Replacements
The i flag ensures all variations of "hello" (case-insensitive) are replaced.
JavaScript
let s = "Hello, hello, HELLO!";
let regex = /hello/gi;
let result = s.replace(regex, "hi");
console.log(result);
4. Filtering Text Inputs
This regex checks if the input matches "username" regardless of its case.
JavaScript
let input = "uSeRNaMe";
let regex = /^username$/i;
if (regex.test(input)) {
console.log("Valid username!");
}
5. Matching Multi-Line Data
The i flag allows case-insensitive matching across lines in combination with the m flag.
JavaScript
let data = `
apple
Banana
CHERRY
`;
let regex = /^banana$/im;
// Matches 'banana' case-insensitively in multi-line mode
console.log(regex.test(data));
Why Use the ignoreCase Property?
The ignoreCase property is indispensable in scenarios requiring flexible, case-agnostic pattern matching, making it a vital tool in any JavaScript developer’s arsenal.
Recommended Links:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4