JavaScript's regular also has a single-line mode.

Source: Internet
Author: User

The regular expression was first implemented by Ken Thompson in his improved QED editor in 1970, the simplest meta-character in the regular "." At the time, the match is any character except for the line break:

"." is a regular expression which matches any character except <nl>.

The above statement is from the official document of QED in 1970, which may be the first regular document in history.

Why do you have to rule? This is because QED edits the file in a behavioral unit, and the line-end newline is also included in the line. For example, if you want to delete all the single-line comments in a piece of code, you can use the following command in QED:

1, $s #//.*##

If the "." Can match the newline character, then the newline character is also deleted, causing the lines to merge with its next line, which is usually not the result we want, so the "." was designed to not match the newline character when it was originally invented. Although there is no QED command on the current operating system to let us test, but we also have Vim,vim "." Also cannot match the newline character because of the same reason.

Unlike in Node, where reading a file is usually a brain that reads through the entire file, Perl inherits the tradition of many Linux commands reading files by line, like this:

while (<>) {print $_}

$_ also has a newline character at the end of the line, so Perl naturally inherits the "." Of the QED. does not match the line break rules. But Perl, after all, is a gate programming language, not an editor, and its regular matching object is not just a single line of text, but it can also be multiple lines of text, so in its regular, "." has a requirement for cross-line matching, so Perl invented the regular single-line mode/s, which allows "." to match line breaks.

The official description of the/s modifier used to open single-line mode in Perl is "Treat the string as", which is understood by "." In normal mode only matches inline characters and cannot span rows; In single-line mode, Perl Will pretend to think of the multiline string as a line, and the newline character in it as the inline characters, so "." can match them. More image Point says that is to put the following three lines of text

123

As the "1\n2\n3\n" line of text, single-line mode is this meaning.

But the thing is, for the same reason (string variables can contain multiple lines of text), Perl also invented the/M modifier, the multi-line mode, the official description is "Treat the string as multiple lines", this pattern of JavaScript in the regular has been, which In this "multi-line" means: ^ and $ metacharacters default does not match a string in the middle of those newline characters before and after the position, that is to say that the string is always only one row, open multi-line mode can be matched.

That is, the single-line mode and multiline mode is for different metacharacters, just contact regular people will be "single-line mode" and "multiline mode" two seemingly is the concept of the opposite, in fact, nothing associated with the noun to get dizzy.

Later, Ruby's author may think that the "single-line mode" of the regular term is not good, the special case alone to let "." Match line breaks this pattern is called "multiline mode", that is, to let. * and the like can match more than one line, so it makes sense that the modifier also uses the/m (the default in Ruby opens "Multi-line mode", so/M is not occupied), this is really worse, more chaotic.

Later, the Python author may also feel that the term "single-line mode" should be avoided, so a new name "Dotall", that is, to allow Dot to match all the characters of the meaning, a good name, and then Java also used the name.

It reviews the history, explains the origin of the next single-line pattern, and explains that the next-line mode is not a good name. V8 recently implemented an ES proposal for Stage 3, Https://github.com/mathiasbynens/es-regexp-dotall-flag, which introduces the/s modifier and dotall for JavaScript. Properties, the Dotall property is learned that Python and java,/s modifiers are inherited by Perl, and there is no need to invent a new modifier such as/d, just to make things more complicated. The specific effect of the/s in JavaScript is to allow "." to match four line terminators that previously did not match: \ n (newline), \ r (carriage return), \u2028 (line delimiter), \u2029 (paragraph delimiter):

// true // true

In fact, it is a very simple thing, but maybe some students who have not touched the regular outside of JavaScript will be confused after learning this new model, here again to clarify: multi-line mode control is ^ and $ performance, single-line mode control is "." Performance, the two are not directly related.

However, the two easy-to-confuse Perl languages, which introduced single-line and multiline mode, have completely removed the two modes in Perl 6: the "." Sign matches the newline character by default, \ n matches any of the characters except the line break, and the ^ and $ always match the first and the end of the string, while the new ^^ and $$ two new To match the line's end and end.

The substitution of the single-line model we used in the past [^] or [\s\s] is not entirely useless, such as in some JavaScript-regular editors (VS Code, Atom), which is unlikely to give you an interface to open single-line mode. But speaking of the regular function in the editor, the regular function of the editor implemented with JavaScript is still too weak, such as the inability to open certain patterns within the regular itself, such as in the case of Sublime (using Python), and using the regular internal (? s) to open the Dotall Patterns, such as the ability to match (? s)/\*.+?\*/to all multi-line annotations.

JavaScript's regular also has a single-line mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.