In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of JavaScript regular single-line mode. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Regular expressions were first implemented by Ken Thompson in his improved QED editor in 1970, the simplest metacharacter in the rules. What was matched at that time was any character except the newline character:
"." Is a regular expression which matches any character except.
The above sentence comes from the official document of QED in 1970, which may be the first regular document in history.
Why is that a rule? This is because QED edits the file in units of behavior, and the newline character at the end of the line is counted in the content of this line. For example, if you want to delete all single-line comments in a piece of code, you can use the following command in QED:
1Be cymbals raceme pinion.
If "." If the newline character can be matched, the newline character will also be deleted, causing these lines to merge with its next line, which is not usually the result we want, so "." It was designed to not match newline characters when it was first invented. Although there are no QED commands for us to test on the current operating system, we still have the "." in VIM,VIM. The newline character cannot be matched either, for the same reason.
Unlike in Node, where reading a file usually means reading the entire file in one fell swoop, Perl inherits the tradition of many Linux commands reading files line by line, like this:
While () {print $_}
There is also a newline character at the end of _, so Perl naturally inherits QED's "." Does not match the rule of newline characters. But Perl is, after all, a programming language, not an editor, and its regular matching objects will be not only single-line text, but also multiple lines of text, so in its rules, "." There is a need for cross-line matching, so Perl invented the regular single-line mode / s, that is, let "." You can also match newline characters.
The official description of the / s modifier used to open single-line mode in Perl is "Treat the string as single line". This "single line" should be understood this way: "." In normal mode, only inline characters can be matched, not across lines, while in single-line mode, Perl pretends to treat multiple-line strings as one line and treats newline characters as inline characters, so. So we can match them. To put it more vividly, it is to put the following three lines of text
one hundred and twenty three
Think of it as "1\ N2\ n3\ n" one line of text, and that's what single-line mode means.
But sadly, for the same reason (string variables can contain multiple lines of text), Perl also invented the / m modifier, the multiline pattern, which is officially described as "Treat the string as multiple lines", which has existed since ancient times in the pattern JavaScript, where the "multiline" means that the ^ and $metacharacters do not match the positions before and after the newline characters in the middle of a string by default. That is, it is thought that the string will always have only one line, and when the multi-line mode is turned on, it can be matched.
In other words, single-line mode and multi-line mode are aimed at different metacharacters, and people who are new to the rule will be confused by the two seemingly corresponding concepts of "single-line mode" and "multi-line mode".
Later, the authors of Ruby may think that the regular term "one-way mode" is not good enough. The pattern of matching newline characters is called "multiline mode", that is, rules such as. * can match multiple lines, so it makes perfect sense, and the modifier also uses / m (the default in Ruby turns on "multiline mode" in Perl, so / m is not occupied), which is really worse and more messy.
Later, the author of Python may have thought that the term "single-line mode" should be avoided and came up with a new name "dotall", which allows dot to match the meaning of all characters, a good name, and later Java also used that name.
The above reviews the history, explains the origin of the one-line mode and shows that the name of the one-line mode is not good. V8 recently implemented a stage 3 ES proposal https://github.com/mathiasbynens/es-regexp-dotall-flag, which introduces the / s modifier and the dotAll attribute to the JavaScript regular. The dotAll attribute learns that the Python and Java,/s modifiers inherit Perl, and there is no need to invent a new modifier such as / d, which will only make things more complicated. The specific effect of / s in JavaScript is to make "." Can match four line Terminators that could not be matched before:\ n (newline),\ r (carriage return),\ u2028 (line delimiter),\ u2029 (paragraph separator):
/ foo/s.dotAll / / true/ ^. {4} $/ s.test ("\ n\ r\ u2028\ u2029") / / true
In fact, it is a very simple thing, but some students who have not come into contact with regularities other than JavaScript may be confused when they learn this new mode. Here is another clarification: the multi-line mode controls the performance of ^ and $, while the single-line mode controls the performance of "." There is no direct relationship between the two.
However, the Perl language, which introduced the confusing concepts of single-line mode and multi-line mode, has completely deleted these two patterns in Perl 6: "." The number matches the newline character by default,\ N can match any character except the newline character; ^ and $always match the beginning and end of the string, and two new metacharacters ^ ^ and $$are introduced to match the beginning and end of the line.
In the past, the commonly used alternatives to single-line mode [^] or [\ s\ S] are not completely useless, for example, in some editors that use JavaScript rules (VS Code, Atom), it is unlikely to provide you with an interface to open single-line mode. However, when it comes to the regular function in the editor, the regular function of the editor implemented in JavaScript is still too weak, such as not being able to open certain modes within the regular itself. For example, if you are in Sublime (using Python regular), using (? s) inside the regular can open dotall mode. For example, you can use (? s) /\ *. +?\ * / to match all multiline comments.
This is the end of the article on "sample analysis of JavaScript regular single-line mode". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.