JavaScript RegExp ignoreCase
Syntax & Examples
RegExp.ignoreCase property
The ignoreCase property of the RegExp object in JavaScript indicates whether the 'i' flag, which enables case-insensitive matching, is set. This property is read-only.
Syntax of RegExp.ignoreCase
The syntax of RegExp.ignoreCase property is:
RegExp.prototype.ignoreCaseThis ignoreCase property of RegExp whether to ignore case while attempting a match in a string. This property is read-only.
Return Type
RegExp.ignoreCase returns value of type Boolean.
✐ Examples
1 Checking if the ignoreCase property is enabled
In JavaScript, we can check if the 'i' flag is enabled for a RegExp object by accessing the ignoreCase property.
For example,
- We create a RegExp object
regexwith the 'i' flag/abc/i. - We check the
ignoreCaseproperty ofregexto see if it is true. - We log the result to the console.
JavaScript Program
const regex = /abc/i;
const isIgnoreCaseEnabled = regex.ignoreCase;
console.log(isIgnoreCaseEnabled);Output
true
2 Comparing ignoreCase property with and without the 'i' flag
In JavaScript, we can compare the ignoreCase property of RegExp objects with and without the 'i' flag.
For example,
- We create a RegExp object
regexWithIwith the 'i' flag/abc/i. - We create another RegExp object
regexWithoutIwithout the 'i' flag/abc/. - We check the
ignoreCaseproperty of both objects and log the results to the console.
JavaScript Program
const regexWithI = /abc/i;
const regexWithoutI = /abc/;
console.log(regexWithI.ignoreCase); // true
console.log(regexWithoutI.ignoreCase); // falseOutput
true false
3 Using ignoreCase property in conditional statements
In JavaScript, we can use the ignoreCase property in conditional statements to perform different actions based on whether the 'i' flag is enabled.
For example,
- We create a RegExp object
regexwith the 'i' flag/abc/i. - We check the
ignoreCaseproperty ofregexin anifstatement. - If the property is true, we log
'Case-insensitive matching is enabled.'to the console; otherwise, we log'Case-insensitive matching is not enabled.'.
JavaScript Program
const regex = /abc/i;
if (regex.ignoreCase) {
console.log('Case-insensitive matching is enabled.');
} else {
console.log('Case-insensitive matching is not enabled.');
}Output
Case-insensitive matching is enabled.
Summary
In this JavaScript tutorial, we learned about ignoreCase property of RegExp: the syntax and few working examples with output and detailed explanation for each example.