Regular expressions were first implemented by Ken Thompson in his improved QED editor in 1970. The simplest metacharacter "." in regularity matched at that time any character except line breaks:
"." is a regular expression which matches any character except <nl>.
The above sentence comes fromQED's official documentation in 1970, this may be the first regular document in history.
Why do you have to make such a regulation? This is because QED edits the file in a behavioral unit, and the line breaks at the end of the line are also included 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:
1,$s#//.*##
If "." can match a newline, the newline will also be deleted, causing these lines to merge with its next line, which is usually not the result we want, so "." was originally designed to not match a newline. Although there is no QED command on the operating system for us to test, we still have VIM, and the "." in VIM cannot match the newline character because of the same reason.
Unlike in Node, reading files is usually a complete file in one go, Perl inherits the tradition of many Linux commands reading files by line, like this:
while (<>) {print $_}
There is also a newline at the end of _, so Perl naturally inherits the "." of QED's "." that does not match the newline. But Perl is a programming language after all, not an editor. The object to which its regularity matches will not only be single-line text, but may also be multi-line text. Therefore, in its regularity, "." has the need for cross-line matching, so Perl invented the regular single-line pattern /s, that is, let "." match line breaks.
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 in this way: "." can only match inline characters in normal mode, not across lines; while in single-line mode, Perl will pretend to regard multiple lines of string as one line, and regard the newline characters in it as inline characters, so "." can match them. To put it more vividly, it is to put the following three lines of text
1 2 3
Considered as "1\n2\n3\n" one-line text, this is what the single-line mode means.
But the worst thing is that for the same reason (string variables can contain multiple lines of text), Perl also invented the /m modifier, that is, the multi-line pattern. The official description is "Treat the string as multiple lines". This pattern has been found in the regular rules of JavaScript since ancient times. Here, the "multi-line" means: the ^ and $ metacharacters will not match the positions before and after the newlines in the middle of a string by default, that is, it is believed that the string will always have only one line, and it can match after opening the multi-line pattern.
In other words, single-line mode and multi-line mode are targeted at different metacharacters. People who are new to regular code will be confused by the two seemingly corresponding concepts, "single-line mode" and "multi-line mode", but in fact there is no correlation.
Later, Ruby's authors may think that the regular term "single-line mode" is not good. In the special case, the pattern of matching the newline with "." is called "multi-line mode", that is, the regulars such as .* can match multiple lines, so it makes completely sense. The modifier also uses /m (the "multi-line mode" in Perl will be turned on by default, so /m is not occupied), which is really a bit insult and even more messy.
Later, the Python author may also think that the name "single-line pattern" should be avoided, so he gave a new name "dotall", which means that dot can match all characters' meanings, a good name. Later, Java also used this name.
The above reviews history, explains the origin of the order line mode, and explains that the name of the order line mode is not well named. V8 has just recently implemented a stage 3 ES proposal/mathiasbynens/es-regexp-dotall-flag, This proposal introduces the /s modifier and dotAll attribute to JavaScript's regularity. The dotAll attribute learns Python and Java. The /s modifier inherits Perl. 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 allow "." to match four previously unmatchedLine Terminator:\n(line break), \r(carriage return), \u2028 (line separator), \u2029 (paragraph separator):
/foo/ // true /^.{4}$/("\n\r\u2028\u2029") // true
In fact, it is just a very simple thing, but some students who have never been exposed to regularities other than JavaScript may become confused when they learn this new mode. Let me clarify here: the multi-line mode controls the performance of ^ and $, and the single-line mode controls the performance of ".", which has no direct relationship between the two.
However, the Perl language, which introduced the concepts of single-line pattern and multi-line pattern, has completely deleted these two patterns in Perl 6: the "." sign matches the newline character by default, and \N can match any character except the newline character; ^ and $ always match the beginning and end of the string, while two metacharacters ^^ and $$ are newly introduced to match the beginning and end of the line.
The alternatives to single-line mode we used in the past [^] or [\s\S] are not completely useless. For example, in some editors that use JavaScript regularity (VS Code, Atom), it is unlikely that you can provide an interface to enable single-line mode. However, when it comes to the regular functions in the editor, the regular functions of the editor implemented in JavaScript are still too weak. For example, some modes cannot be enabled inside the regular itself. For example, if you are in Sublime (using Python regulars), you can use (?s) inside the regular to enable dotall mode, for example, you can use (?s)/\*.+?\*/ to match all multi-line comments.