JavaScript ignoreCase Property RegExp.ignoreCase Property (Regular Expressions)

This tutorial covers the ignoreCase property, part of JavaScript's RegExp object. By default, regular expressions are case-sensitive: a lowercase 'a' would not match an uppercase 'A', for example. The ignoreCase property (abridged with the 'i' flag) renders regular expressions case-insensitive: in other words, regular expressions flagged with ignoreCase will match words or characters without regards for their capitalization.

Using the ignoreCase Property

To illustrate the concept, let us look a the following piece of script (which uses the test() method to check if a string contains a pattern).

// Match a regular expression pattern with the test() method
alert( /javascript/.test("I learn JavaScript") );

Somewhat counter-intuitively, the code above returned: JavaScript ignoreCase property: false by default

The ultra-simple regular expression expression we declared did not match the obviously present word "javascript": the ignoreCase property is optional, with a default value of false. The script below, on the other hand, would return true.

alert( /javascript/i.test("I learn JavaScript") ); // returns true

The only difference in our two JavaScript snippets is the "i" character following the regular expression literal in the second script. "Turning on" ignoreCase (or setting the ignoreCase property to "true") is as simple as appending the letter i. We declare below the same regular expression, once with the constructor, and once with literal notation. Both regular expressions with be case-insensitive:

var remo1 = /javascript/i;
var remo2 = new RegExp("javascript","i");

The two regular expressions we declared are identical, and have both their ignoreCase property set to true.

Why Use the ignoreCase Property?

Below is example of the advantages of using the ignoreCase property. The two regular expressions declared below attempt to match a range of characters; in this case, the letters of the alphabet.

// Declare a case-insensitive regular expression
// without using the ignoreCase property
var remo1 = /[a-zA-Z]/;

// Match the same pattern, using "i" this time
var remo2 = /[a-z]/i;

Remember: square brackets […] are used in regular expressions to include a set of characters; hyphenated, these characters are interpreted as a range.

Using the ignoreCase property in your scripts makes them easier to read, and the length your regular expression patterns is cut by half.

Test the ignoreCase Property

Interactively test the ignoreCase property by editing the JavaScript code below and clicking the Test ignoreCase Property button.

Browser support for JavaScript ignoreCase property
Internet Explorer supports the JavaScript ignoreCase propertyFirefox supports the JavaScript ignoreCase propertySafari supports the JavaScript ignoreCase propertyOpera supports the JavaScript ignoreCase property