


What is single-line mode? Detailed explanation of JavaScript regular single-line mode
This article mainly introduces JavaScript regular expressions which also have single-line mode. Friends who need it can refer to it
Regular expressions were first implemented by Ken Thompson in his improved QED editor in 1970. , the simplest metacharacter in the regular expression "." at that time matched any character except the newline character:
"." is a regular expression which matches any character except
The above sentence comes from QED's official document in 1970, which may be the first regular document in history.
Why is this stipulated? This is because QED edits files in line units, and the newline character at the end of the line is 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 the newline character, then the newline character will also be deleted, and it will Causes these lines to be merged with the next line, which is usually not what we want. Therefore, "." was designed not to match newlines when it was originally invented. Although there is no QED command on the current operating system for us to test, we still have VIM, and the "." in VIM cannot match the newline character for the same reason.
Unlike in Node, where reading a file usually involves reading the entire file in one go, Perl inherits the tradition of many Linux commands reading files line by line, like this:
while () {print $_}
_ The end of There are also newlines, so Perl naturally inherits QED's rule that "." does not match newlines. But Perl is a programming language after all, not an editor. The objects that its regular expressions need to match are not only single lines of text, but may also be multi-line texts. Therefore, in its regular expressions, "." has a requirement for cross-line matching. Therefore, Perl invented the regular single-line mode /s, which allows "." to also match newline characters.
The official description of the /s modifier in Perl used to turn on single line mode is "Treat the string as single line". This "single line" should be understood like this: "." can only match in normal mode. Inline characters cannot span lines; in single-line mode, Perl will pretend to treat multi-line strings as one line, and treat the newline characters as inline characters, so "." can match them. To put it more vividly, the following three lines of text
1 2 3
are regarded as "1\n2\n3\n" one line of text. This is what single-line mode means.
But the terrible thing is that for the same reason (string variables can contain multiple lines of text), Perl also invented the /m modifier, which is multi-line mode. The official description is "Treat the string as multiple lines ", this pattern has been included in the regular JavaScript rules since ancient times. The "multiple lines" here means: ^ and $ metacharacters will not match the positions before and after the newline characters in the middle of a string by default, that is, the string is always considered to be only one line. , you can match after turning on multi-line mode.
In other words, single-line mode and multi-line mode are for different metacharacters. People who are new to regular expressions will be confused by the two seemingly corresponding "single-line mode" and "multi-line mode". concept, but in fact, it is confusing with unrelated terms.
Later, the author of Ruby may have felt that the regular term "single-line mode" was not used well, so he called the pattern of "." matching newlines "multi-line mode", that is, let . * and other regular expressions can match multiple lines, so it makes perfect sense. The modifier also uses /m (Ruby will enable the "multiline mode" in Perl by default, so /m is not occupied). This is really To add insult to injury, it’s even more chaotic.
Later, the Python author may also feel that the term "single-line mode" should be avoided, so he gave a new name "dotall", which means that dot can match all characters. It is a good name. , and later Java also used this name.
The above has reviewed the history, explained the origin of the single-line mode, and explained that the name of the single-line mode was not chosen well. V8 has recently implemented a stage 3 ES proposal github.com/mathiasbynens/es-regexp-dotall-flag. This proposal introduces the /s modifier and dotAll attribute to JavaScript regularity. The dotAll attribute is learned from Python and Java. The /s modifier is inherited from Perl. There is no need to invent a new modifier such as /d here, which will only make things more complicated. The specific effect of /s in JavaScript is to allow "." to match four line terminators that could not be matched before: \n (line feed), \r (carriage return), \u2028 (line separator), \u2029 (paragraph separator) symbol):
/foo/s.dotAll // true /^.{4}$/s.test("\n\r\u2028\u2029") // true
is actually a very simple thing, but some students who have not been exposed to regular expressions other than JavaScript may have problems when they learn this new pattern. Confused, let me clarify again: multi-line mode controls the performance of ^ and $, and single-line mode controls the performance of ".". There is no direct relationship between the two.
However, the Perl language, which originally introduced the confusing concepts of single-line mode and multi-line mode, has completely deleted these two modes in Perl 6: "." matches the newline character by default, \N Can match any character except newline; ^ and $ always match the beginning and end of the string, while the two new metacharacters ^^ and $$ are introduced to match the beginning and end of the line.
The single-line mode alternatives [^] or [\s\S] that we commonly used in the past are not completely useless. For example, in some editors that use JavaScript regularity (VS Code, Atom), it is unlikely to give You provide an interface to enable single-line mode. However, talking about the regular function in the editor, the regular function of the editor implemented in JavaScript is still too weak. For example, certain modes cannot be turned on within the regular code itself. For example, if it is in Sublime (using Python regular code), inside the regular code Use (?s) to enable dotall mode. For example, you can use (?s)/\*.+?\*/ to match all multi-line comments.
The above is the detailed content of What is single-line mode? Detailed explanation of JavaScript regular single-line mode. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment