RegExp.prototype.ignoreCase

Returns whether the regular expression was created with the i flag.

Signature
get RegExp.prototype.ignoreCase
  • Added in JavaScript ES3 (1999)

Return value

booleantrue if the i flag is set, false otherwise.

Errors

TypeError if accessed on a value that is not a RegExp object (other than RegExp.prototype itself).

Changelog

  • PHP ES6ignoreCase became an accessor on RegExp.prototype rather than an own data property of each instance.
  • PHP ES2015Combining i with the u flag switches matching to Unicode simple case folding, which changes results for characters such as the Kelvin sign.

ignoreCase reports whether the i flag is present. It is read-only, so treat it as a query about a regex you were handed.

What i actually does

Both the pattern and the input are folded to a common case before comparison. That extends to character classes, so /[a-z]+/i matches "HELLO" — a frequent surprise when the class was meant as a lowercase-only guard. Write [a-zA-Z] explicitly and drop the flag when the case genuinely matters.

Simple folding, not full folding

The specification uses Unicode simple case folding, which maps one code point to one code point. There is no expansion, so /ß/i does not match "ss", and it does not even match capital ẞ. Anything requiring locale rules — Turkish dotless i, for example — is out of scope entirely; use Intl.Collator with a sensitivity setting instead.

Adding the u flag switches to the Unicode folding table, which brings in mappings the legacy path skips: /k/iu matches the Kelvin sign K (U+212A) while /k/i does not.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Match a user's search term regardless of how they capitalised it
  • Accept header names or keywords in any casing while parsing
  • Warn when a caller passes a case-insensitive regex to a case-sensitive comparison

Frequently asked questions

How do I make a regex case-insensitive in JavaScript?

Add the i flag: /cat/i, or new RegExp(pattern, "i"). The flag cannot be toggled after construction, so build a new regex with new RegExp(re.source, re.flags + "i") if you need to add it later.

Why does /[a-z]/i match uppercase letters?

The i flag folds the character class as well as the input, so a-z covers A-Z too. If you need a genuinely lowercase-only check, drop the flag rather than trying to narrow the class.

Does the i flag handle every language correctly?

No. It applies Unicode simple case folding with no locale awareness, so ß does not match ss and Turkish dotted or dotless i rules are ignored. Use Intl.Collator with sensitivity "accent" or "base" for locale-correct comparison.

More examples

Simple folding has limits

Case-insensitive matching maps one code point to one code point, so multi-character equivalences never apply. Adding u is what brings compatibility characters like the Kelvin sign into the fold.

Example · javascript
console.log(/ß/i.test("ss")); // false - no expansion
console.log(/ß/i.test("ẞ"));  // false - not in simple folding

// The u flag switches to the Unicode folding table:
console.log(/k/i.test("K"));  // false (U+212A KELVIN SIGN)
console.log(/k/iu.test("K")); // true

console.log(/straße/i.test("STRASSE")); // false

Escaping a search term for a highlighter

The i flag keeps both "Cat" and "cat" while $& preserves the original casing in the output. Escaping matters because the second term contains $, . and ( which would otherwise be regex syntax.

Example · javascript
const escape = s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

function highlight(text, term) {
  const re = new RegExp(escape(term), "gi");
  return text.replace(re, m => `[${m}]`);
}

console.log(highlight("The Cat and the cat.", "cat"));
console.log(highlight("Price: $5.00 (net)", "$5.00"));

Discussion

  • Be the first to comment on this lesson.