Multiline Mode of Anchors $, Flag m
Multiline Mode of Anchors ^ $, Flag “m” (锚点^ $的多行模式,标志“m”)
In this chapter, you will learn when and how to use the multiline mode of the anchors in JavaScript. (在本章中,您将学习何时以及如何在JavaScript中使用锚点的多行模式。)
The multiline property is a read-only property of an individual regexp instance. It specifies whether or not the m flag is applied inside the regexp. (多行属性是单个正则表达式实例的只读属性。它指定是否在正则表达式内应用m标志。)
So, for enabling the multiline mode the m flag is used. (因此,对于启用多行模式,使用m标志。)
The value of the multiline property is boolean: when the m flag is used, it’s true, otherwise- false. The m flag specifies that a multiline input string should be dealt with as multiple lines.
In the multiline mode, the anchors ^ and $ match not only at the start and the end of the string, but also at the beginning/end of the line. (在多行模式下,锚点^和$不仅在字符串的开始和结束处匹配,而且在行的开始/结束处匹配。)
Searching at Line Start ^
Searching at Line Start ^ (在行首搜索^)
Now, let’s observe an example of using multiple lines. The pattern /^\d/gm takes a digit from the start of every line, like this:
let str = `1st: Javascrip
2nd: Css
3rd: Html`;
console.log(str.match(/^\d/gm)); // 1, 2, 3
Only the first digit will be matched without using the m flag, as shown below:
let str = `1st: Javascript
2nd: Css
3rd: Html`;
console.log(str.match(/^\d/g)); // 1
The reason is that the ^ caret only matches at the start of the text, and within the multiline mode- at the beginning of any line. (原因是^插入符号仅在文本开头匹配,并且在任何行的开头在多行模式内匹配。)
Note that, the start of the line formally considers “right after a line break”. In the multiline mode the test ^ matches at all the positions, preceded by a newline character \n . It happens at the beginning of the text. (>请注意,行首正式考虑“在换行之后”。在多行模式下,测试^在所有位置都匹配,前面有一个换行符\ n。它发生在文本的开头。)
Searching at Line End $
Searching at Line End $ (正在$行尾搜索)
Now, let’s see how the dollar sign $ behaves. (现在,让我们来看看美元符号$的行为。)
The regexp \d$ can find the last digit in each line, like this:
let str = `Javascript: 1
Css: 2
Html: 3`;
console.log(str.match(/\d$/gm)); // 1,2,3
Without using the m flag, the dollar sign $ could only match at the end of the text. Hence, only the last digit would be found. (如果不使用m标志,美元符号$只能在文本末尾匹配。因此,只能找到最后一位数字。)
Note that the end of a line formally considers “ at once before a line break”. In the multiline mode, the dollar sign $ matches at all the positions succeeded by a newline character \n. This happens at the end of the text. (>请注意,行尾正式考虑“在换行之前立即”。在多行模式下,美元符号$在由换行符继承的所有位置匹配\ n。这发生在文本的末尾。)
Searching for \n Instead of ^ $
Searching for \n Instead of ^ $ (正在搜索\ n而不是^ $)
For finding a newline, you can use not only the anchors ^ and $ but the newline character \n, as well. (要查找换行符,您不仅可以使用锚点^和$ ,还可以使用换行符\ n。)
To reveal the difference, let’s consider an example where the search is for \d\n instead of \d$:
let str = `Javascript: 1
Css: 2
Html: 3`;
console.log(str.match(/\d\n/gm)); // 1\n,2\n
So, in the example above, instead of three matches, there are two. (因此,在上面的示例中,不是三场比赛,而是两场比赛。)
The reason is that no newline exists after 3. The next difference is that now each match includes a newline character \n. In contrast to the anchors ^ $ (they only test the condition beginning/end of a line) \n is a character. Therefore, it is part of the result. (原因是3之后不存在换行符。下一个区别是,现在每场比赛都包含一个换行符\ n。与锚点^ $相反(它们仅测试行开头/结尾的条件)\ n是一个字符。因此,它是结果的一部分。)
As a conclusion, let’s note that in the pattern, \n is used when it’s necessary to have newline characters in the result. The anchors, on their turn, are used for finding something at the start/end of a line. (总而言之,请注意,在模式中,\ n在结果中需要使用换行符时使用。锚点轮流用于在线条的开头/结尾找到一些东西。)