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