JavaScript RegExp unicodeSets
Syntax & Examples
RegExp.unicodeSets property
The unicodeSets property of the RegExp object in JavaScript indicates whether the 'v' flag, which is an upgrade to the 'u' mode for handling Unicode sets, is enabled. This property is read-only.
Syntax of RegExp.unicodeSets
The syntax of RegExp.unicodeSets property is:
RegExp.prototype.unicodeSets
This unicodeSets property of RegExp whether or not the 'v' flag, an upgrade to the 'u' mode, is enabled. This property is read-only.
Return Type
RegExp.unicodeSets returns value of type Boolean
.
✐ Examples
1 Checking if the unicodeSets property is enabled
In JavaScript, we can check if the 'v' flag is enabled for a RegExp object by accessing the unicodeSets
property.
For example,
- We create a RegExp object
regex
with the 'v' flag/\p{Letter}/v
. - We check the
unicodeSets
property ofregex
to see if it is true. - We log the result to the console.
JavaScript Program
const regex = /\p{Letter}/v;
const isUnicodeSetsEnabled = regex.unicodeSets;
console.log(isUnicodeSetsEnabled);
Output
true
2 Comparing unicodeSets property with different flags
In JavaScript, we can compare the unicodeSets
property of RegExp objects with and without the 'v' flag.
For example,
- We create a RegExp object
regexWithV
with the 'v' flag/\p{Letter}/v
. - We create another RegExp object
regexWithoutV
without the 'v' flag/\p{Letter}/u
. - We check the
unicodeSets
property of both objects and log the results to the console.
JavaScript Program
const regexWithV = /\p{Letter}/v;
const regexWithoutV = /\p{Letter}/u;
console.log(regexWithV.unicodeSets); // true
console.log(regexWithoutV.unicodeSets); // false
Output
true false
3 Using unicodeSets property in conditional statements
In JavaScript, we can use the unicodeSets
property in conditional statements to perform different actions based on whether the 'v' flag is enabled.
For example,
- We create a RegExp object
regex
with the 'v' flag/\p{Letter}/v
. - We check the
unicodeSets
property ofregex
in anif
statement. - If the property is true, we log
'Unicode sets are enabled.'
to the console; otherwise, we log'Unicode sets are not enabled.'
.
JavaScript Program
const regex = /\p{Letter}/v;
if (regex.unicodeSets) {
console.log('Unicode sets are enabled.');
} else {
console.log('Unicode sets are not enabled.');
}
Output
Unicode sets are enabled.
Summary
In this JavaScript tutorial, we learned about unicodeSets property of RegExp: the syntax and few working examples with output and detailed explanation for each example.