Regular Expression u modifier (UTF-16 encoding supported), regular expression UTF-16
Regular Expression u modifier:
This modifier identifies Unicode characters greater than \ uFFFF.
That is, the UTF-16 encoding of four bytes is processed correctly.
This modifier is added to ES2015. For more features of regular expressions, see section 1 "New Features of ES2015 regular expressions.
For more regular expression tutorials, see the regular expression tutorial section.
Code example:
Console. log (/^ \ uD842/u. test ("\ uD842 \ uDFB7 "))
The output is false, because "\ uD842 \ uDFB7" is a four-byte UTF-16 encoded, representing a character, so if the regular expression carries a u modifier, it can be recognized.
Console. log (/^ \ uD842/. test ("\ uD842 \ uDFB7 "))
Output true; without the u modifier, the four-byte UTF-16 encoding cannot be recognized as a character, so a match can be produced.
/^. $/. Test ("\ uD842 \ uDFB7") // false/^. $/u. test ("\ uD842 \ uDFB7")/true
For usage of metacharacters (.), see section 1 of the regular expression. No. metacharacters.
/^. $/. Test ("\ uD842 \ uDFB7") // false/^. $/u. test ("\ uD842 \ uDFB7")/true
After the u modifier is added, the dot metacharacters can match Unicode characters with a code point greater than 0xFFFF.
/\ U {61}/. test ("a") // false/\ u {61}/u. test ("a") // true
Using the u modifier, the regular expression can recognize Unicode characters represented by braces {}. Otherwise, the regular expression cannot be recognized. {61} will also be interpreted as quantifiers, representing 61 u characters.
For more information about Unicode characters in braces, see the section "ES2015 new feature.