The meaning and use of regular expressions in JavaScript
I believe that many people know regular expressions, but many people’s first impression is that it is difficult to learn, because at first glance, they feel that there is no pattern at all, and it is all a bunch of various All kinds of special symbols are completely incomprehensible. In fact, regular expressions are not as difficult as you think. Today we will take you to quickly understand JavaScriptregular expressions!
1. What is a regular expression?
Regular expression is a special The string pattern is used to match a set of strings, which is like using a mold to make a product, and regular rules are this mold, defining a rule to match characters that match the rules.
Regular expression (regular expression) describes a string matching pattern (pattern), which can be used to check whether a string contains a certain substring, replace the matching substring, or select from a certain string. Get substrings that meet certain conditions, etc.
To put it bluntly, regular expressions are used to process strings. We can use it to process some complex strings.
2. Regular expression rules
##1.1 Ordinary characters Letters, numbers, Chinese characters, underscores, and punctuation marks not specifically defined in the following chapters are all "ordinary characters". Ordinary characters in an expression, when matching a string, match the same character.
Example 1: The expression "c", when matching the string "abcde", the matching result is: success; the matched content is: "c"; the matched position is: starting at 2, Ended at 3. (Note: Whether the subscript starts from 0 or 1 may differ depending on the current
programming language)
Example 2: The expression "bcd" matches the string "abcde" ", the matching result is: success; the matched content is: "bcd"; the matched position is: starting at 1 and ending at 4.
3. Special characters in regular expressions
Character meaning
\ is used as a change of meaning, that is, the characters after "\" are usually not interpreted according to their original meaning. For example, /b/ matches the character "b". When a backslash is added in front of b, /\b/ will change the meaning. To match a word boundary.
-or-
Restoration of regular expression functional characters, such as "*" matching the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, with "\" added After that, /a\*/ will only match "a*".
^ matches an input or the beginning of a line, /^a/ matches "an A", but does not match "An a"
$ matches an input or the end of a line, /a$/ matches " An a", not matching "an A"
* Matches the preceding metacharacter 0 or more times, /ba*/ will match b,ba,baa,baaa
+ Matches the preceding metacharacter 1 or more times times, /ba*/ will match ba,baa,baaa
? Match the preceding metacharacter 0 or 1 times, /ba*/ will match b,ba
(x) Match x and save x in a file named $1 ...in the variable of $9
x|y matches x or y
{n} matches exactly n times
{n,} matches n or more times
{n,m} matches n-m times
[xyz] character set, matches any character (or metacharacter) in this set
[^xyz] does not match any character in this set
[\b] Matches a backspace character
\b Matches a word boundary
\B Matches a non-word boundary
\cX Here, X is a control character, /\cM/ matches Ctrl-M
\d matches an alphanumeric character, /\d/ = /[0-9]/
\D matches a non-alphanumeric character, /\D/ = /[^0-9]/
\n matches a Line feed character
\r matches a carriage return character
\s matches a blank character, including \n,\r,\f,\t,\v, etc.
\S matches a non-blank character, equal to /[^\n\f\r\t\v]/
\t Matches a tab character
\v Matches a double tab character
\w Matches a character that can form a word ( alphanumeric, this is my free translation, including numbers), including underscores, such as [\w] matches the 5 in "$5.98", which is equal to [a-zA-Z0-9]
\W matches a word that cannot be formed Characters, such as [\W] matches the $ in "$5.98", which is equal to [^a-zA-Z0-9].
4. Basic syntax of regular expressions
Two special symbols '^' and ' $'. Their function is to indicate the beginning and end of a string respectively.
Examples are as follows:
"^The": represents all strings starting with "The" ("There", "The cat", etc.);
"of despair$": indicates a string ending with "of despair";
"^abc$": indicates a string starting and ending with "abc" - haha, there is only "abc" itself. ;
"notice": Represents any string containing "notice".
Like the last example, if you don't use two special characters, you are indicating that the string you want to find is in any part of the searched string - you are not
positioning it at the top of a certain .
Other symbols include '*', '+' and '?', which represent the number of times a character or a sequence of characters appears repeatedly.
They mean "none or more", "once or more" and "none or once" respectively.
Here are a few examples:
"ab*": Indicates that a string has an a followed by zero or several b. ("a", "ab", "abbb",...);
"ab+": Indicates that a string has an a followed by at least one b or more;
" ab?": Indicates that a string has an a followed by zero or one b;
"a?b+$": Indicates that there is zero or one a followed by one or several b at the end of the string .
You can also use a range, enclosed in curly brackets, to indicate the range of repetitions.
"ab{2}": Indicates that a string has an a followed by 2 b ("abb");
"ab{2,}": Indicates that a string has a a followed by at least 2 b;
"ab{3,5}": Indicates that a string has an a followed by 3 to 5 b.
Please note that you must specify the lower limit of the range (eg: "{0,2}" instead of "{,2}").
Also, you may have noticed that '*', '+' and '?' are equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a '¦', which means "or" operation:
"hi¦hello": indicates that there is "hi" or "hello" in a string;
"(b¦cd)ef": represents "bef" or "cdef";
"(a¦b)*c": represents a string of mixed "a" and "b" followed by A "c";
'.' can replace any character:
"a.[0-9]": Indicates that a string has an "a" followed by an arbitrary character and A number;
"^.{3}$": represents a string of any three characters (length is 3 characters);
Square brackets indicate that certain characters are allowed to appear at a specific position in a string:
"[ab]": Indicates that a string has an "a" or "b" (equivalent to "a¦b");
"[a-d]": Indicates that a string contains one of lowercase 'a' to 'd' (equivalent to "a¦b¦c¦d" or " [abcd]");
"^[a-zA-Z]": represents a string starting with a letter;
"[0-9]%": represents a hundred There is one digit before the semicolon;
",[a-zA-Z0-9]$": indicates that a string ends with a comma followed by a letter or number.
You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first one in the square brackets.
(For example: "%[^a-zA-Z]%" means that letters should not appear between two percent signs).
In order to express verbatim, you must add the transfer character '\' before the characters "^.$()¦*+?{\".
Please note that within square brackets, no escape characters are required.
Summary
In fact, I just don’t understand regular expressions, so I understand. You will find that this is it. There are not many related characters used in regular expressions, and they are not difficult to remember or understand. The only difficulty is that after combining them, the readability is relatively poor and it is not easy to understand. This article It is designed to let everyone have a basic understanding of regular expressions, be able to understand simple regular expressions, and write simple regular expressions to meet the needs of daily development.
Related recommendations:
What are the commonly used regular expressions in js
JavaScript regular expression video tutorial
Definition and introduction of javascript regular expression
How to be flexibleUse JavaScriptRegular expression
The above is the detailed content of The meaning and use of regular expressions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools