search
HomeWeb Front-endJS TutorialThe meaning and use of regular expressions in JavaScript

The meaning and use of regular expressions in JavaScript

Nov 09, 2017 pm 04:52 PM
javascriptjsexpression

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)