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

What are the categories of use of periods in regular expressions?

php中世界最好的语言
Release: 2018-06-09 14:38:01
Original
1410 people have browsed it

This time I will bring you what are the categories of periods used in regular expressions, and what are the precautions for using periods in regular expressions. The following is a practical case, let's take a look.

We know that in regular expressions, . can be used to represent any single character, but in the source code of underscore and jquery, we can see that . is often not used in the codes of these famous libraries. To represent any character, use [\w\W] or [\s\S] instead. At first glance, it seems that the meaning of the expression is the same, but why not use the simple method and go around in more circles? Let me briefly talk about this issue today.

First of all, we must correctly understand the meaning of . In fact, it may be misleading to say that it represents any single character. It must be emphasized that this "any single character" does not include the characters that control line breaks, that is, it does not include the characters \n \r \u2028 or \u2029 . These characters can be included in \W and \s. The difference between the two writing methods is very clear, that is, whether they can match several newline control characters.

So when do we need to consider these newline control characters? When the string to be processed may contain newlines. There are too many such scenarios, processing HTML strings, processing templates, nodejs reading text, etc.

When it comes to multi-line text scenarios, we can easily think of the m mode (multi-line mode) of regular expressions. So does multi-line mode have any impact on the issues we discuss today? I'm not sure. Why not sure? Some people swear that the meaning of . in single-line mode is different from that in multi-line mode. In single-line mode, it is equivalent to [\w\W] or [\s\S], while in multi-line mode, the newline control character is excluded. But according to my experiments and referring to MDN, this is wrong. It is true that regular expressions in many languages ​​have the above characteristics, but I have not seen them in JavaScript. I don’t know if there are differences in browsers. So what impact does multi-line mode have on JavaScript? I think it just changes the meaning of the ^ and $ flags: in single-line mode, they represent the beginning and end of the entire string respectively; in multi-line mode, they represent the beginning and end of each line. Regardless of multi-line mode or single-line mode, I think . does not contain newline control characters and is equivalent to [^\n\r\u2028\u2029].

Extension a little further, for modern browsers, you can directly use [^] to match any character.

I don’t want to write the example program. If you are interested, you can try it yourself. Use /.*/g, /^.*$/g, /.*/gm, /^.*$/gm respectively. Match "abc\nedf", the reason is self-evident.

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:

How to use JS event delegation in the project

Use regular expressions to verify password strength (two methods Code attached)

The above is the detailed content of What are the categories of use of periods in regular expressions?. 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