Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of u modifier in regular expressions (with code)

php中世界最好的语言
Release: 2018-03-30 13:31:33
Original
5437 people have browsed it

This time I will bring you Regular expressiondetailed explanation of the use of u modifier (with code), what are the precautions for using the u modifier of regular expression, the following are Let’s take a look at practical cases.

Regular expression u modifier:

This modifier identifies the ability to correctly handle Unicode characters larger than \uFFFF.
In other words, four-byte UTF-16 encoding will be processed correctly.
This modifier is new in ES2015. For more new features of regular expressions, please refer to the chapter "New Features of Regular Expressions in ES2015".
For more regular expression tutorials, please refer to the regular expression tutorial section.

Code example:

console.log(/^\uD842/u.test("\uD842\uDFB7"))
Copy after login

Output false, because "\uD842\uDFB7" is a four-byte UTF-16 encoding, representing a character, so if the regular expression has u modifier, then you will be able to identify it.

console.log(/^\uD842/.test("\uD842\uDFB7"))
Copy after login

Output true; without adding the u modifier, the four-byte UTF-16 encoding cannot be recognized as one character, so a match can be generated.

/^.$/.test("\uD842\uDFB7")//false
/^.$/u.test("\uD842\uDFB7")//true
Copy after login
Copy after login

For the usage of metacharactersdot (.), please refer to the chapter of regular expressions.dot metacharacter.

/^.$/.test("\uD842\uDFB7")//false
/^.$/u.test("\uD842\uDFB7")//true
Copy after login
Copy after login

After adding the u modifier, dot metacharacters can match Unicode characters with code points greater than 0xFFFF.

/ \u{61} /.test("a")//false
/ \u{61} / u.test("a")//true
Copy after login

Using the u modifier, the regular expression can recognize the Unicode characters represented by braces {}, otherwise it cannot be recognized. {61} will also be interpreted as a quantifier, indicating 61 u character.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Regular Expressions Detailed explanation of the use of \W metacharacters (with code)

Regular Expressions Detailed explanation of the use of . metacharacter (with code)

The above is the detailed content of Detailed explanation of the use of u modifier in regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!