DEV Community

Raju Saha
Raju Saha

Posted on

New Feature in ECMAScript 2024 - New Regular Expression Flag /v (.unicodeSets) 👨‍🎨

JavaScript’s regular expression capabilities have just received a powerful upgrade with the introduction of the /v flag, also known as .unicodeSets. This new flag enables more advanced and flexible pattern matching with Unicode properties, string literals in character classes, and set operations. Let's dive into what /v brings to the table and explore some practical examples.

Unicode String Properties

With the /v flag, you can now use Unicode string properties to match more complex characters. For instance, the character 🧑‍💻 consists of three code points, making it challenging to match with traditional Unicode code point properties.

Example:

//Previously, using the Unicode code point property Emoji via /u
console.log(/^\p{Emoji}$/u.test('🧑‍💻'));  //false

//Now, with the Unicode string property RGI_Emoji via /v
console.log(/^\p{RGI_Emoji}$/v.test('🧑‍💻')); //true

Enter fullscreen mode Exit fullscreen mode

String Literals in Character Classes

The /v flag allows string literals to be used within character classes using the \q{} escape sequence. This makes it easier to match specific sequences directly.

Examples:

//Matching the string literal 🧑‍💻
console.log(/^[\q{🧑‍💻}]$/v.test('🧑‍💻')); // true

//Matching the string literal "abc" or "def"
console.log(/^[\q{abc|def}]$/v.test('abc')); //true
Enter fullscreen mode Exit fullscreen mode

Set Operations for Character Classes

Set operations within character classes are now possible with /v, allowing for more complex matching patterns.

Examples:

//Excluding specific characters
console.log(/^[\w--[a-g]]$/v.test('a')); // false

//Excluding specific digits
console.log(/^[\p{Number}--[0-9]]$/v.test('٣')); // true

//Excluding a specific emoji
console.log(/^[\p{RGI_Emoji}--\q{🧑‍💻}]$/v.test('🧑‍💻')); // false

Enter fullscreen mode Exit fullscreen mode

Improved Matching with /i and Negated Unicode Property Escapes

The /v flag also improves matching when using the /i flag and negated Unicode property escapes.

Conclusion

The introduction of the /v (.unicodeSets) flag in JavaScript’s regular expressions marks a significant improvement in handling Unicode characters, string literals, and set operations. Whether you're dealing with complex emoji sequences or need more precise pattern matching, /v opens up new possibilities.

Have you tried out the new /v flag yet? Share your experiences and let us know how it's helped you in your projects!

Reference Link : Ecma International approves ECMAScript 2024: What’s new?

Happy coding!

Top comments (1)

Collapse
 
raz41 profile image
Razor

hi bro I can contact you to ask some advice on javascript