How to use the regular expression u modifier
Xiaobian to share with you how to use the regular expression u modifier, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
Regular expression u modifier:
This modifier identifies the correct handling of Unicode characters greater than\ uFFFF.
That is, the four-byte UTF-16 encoding is handled correctly.
This modifier is new to ES2015. For more new features in regular expressions, see the section on adding features to regular expressions in ES2015.
More regular expression tutorials can be found in the regular expression tutorials section.
Code example:
Console.log (/ ^\ uD842/u.test ("\ uD842\ uDFB7"))
Output false, because "\ uD842\ uDFB7" is a four-byte UTF-16 encoding that represents a character, so if the regular expression has a u modifier, it can be recognized.
Console.log (/ ^\ uD842/.test ("\ uD842\ uDFB7"))
The output true; without the u modifier makes it impossible to recognize the four-byte UTF-16 encoding as a single character, so a match can be produced.
/ ^. $/ .test ("\ uD842\ uDFB7") / / false/ ^. $/ u.test ("\ uD842\ uDFB7") / / true
For the use of metacharacter dots (.), see regular expressions. The chapter of metacharacters.
/ ^. $/ .test ("\ uD842\ uDFB7") / / false/ ^. $/ u.test ("\ uD842\ uDFB7") / / true
After the u modifier is added, the dot metacharacter can match Unicode characters whose code point is greater than 0xFFFF.
/\ u {61} / .test ("a") / / false/\ u {61} / u.test ("a") / / true
Using the u modifier, the regular expression can recognize the Unicode character represented by the curly braces {}, otherwise it cannot be recognized, and {61} will also be interpreted as a quantifier, representing 61 u characters.
The above is all the content of the article "how to use the regular expression u modifier". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!