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.
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.
RegExp.ignoreCase returns value of type Boolean
.
In JavaScript, we can check if the 'i' flag is enabled for a RegExp object by accessing the ignoreCase
property.
For example,
regex
with the 'i' flag /abc/i
.ignoreCase
property of regex
to see if it is true.const regex = /abc/i;
const isIgnoreCaseEnabled = regex.ignoreCase;
console.log(isIgnoreCaseEnabled);
true
In JavaScript, we can compare the ignoreCase
property of RegExp objects with and without the 'i' flag.
For example,
regexWithI
with the 'i' flag /abc/i
.regexWithoutI
without the 'i' flag /abc/
.ignoreCase
property of both objects and log the results to the console.const regexWithI = /abc/i;
const regexWithoutI = /abc/;
console.log(regexWithI.ignoreCase); // true
console.log(regexWithoutI.ignoreCase); // false
true false
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,
regex
with the 'i' flag /abc/i
.ignoreCase
property of regex
in an if
statement.'Case-insensitive matching is enabled.'
to the console; otherwise, we log 'Case-insensitive matching is not enabled.'
.const regex = /abc/i;
if (regex.ignoreCase) {
console.log('Case-insensitive matching is enabled.');
} else {
console.log('Case-insensitive matching is not enabled.');
}
Case-insensitive matching is enabled.
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.