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.ignoreCase
This 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
regex
with the 'i' flag/abc/i
. - We check the
ignoreCase
property ofregex
to 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
regexWithI
with the 'i' flag/abc/i
. - We create another RegExp object
regexWithoutI
without the 'i' flag/abc/
. - We check the
ignoreCase
property 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); // false
Output
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
regex
with the 'i' flag/abc/i
. - We check the
ignoreCase
property ofregex
in anif
statement. - 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.