Table of Contents
Regular grouping
Repeated matching
Backreference (reverse reference)
Regular Assertion
Precede assertion
Post assertion
Practice
"Positive and negative" assertion
Home Web Front-end JS Tutorial Analysis of key points of JS regular expressions

Analysis of key points of JS regular expressions

Jan 30, 2018 pm 05:20 PM
javascript analyze expression

This article mainly shares with you the key points of JS regular expressions. Since I was looking at the source code of VueJS before and saw the HtmlParser part, I felt that the basic knowledge of regular expressions I had seen before was completely insufficient. Now I read through the blog information and put some information into it. The difficult-to-use parts of regular expressions in JS are summarized to make it easier for you and your SF friends to read.

Regular grouping

Repeated matching

For repeated matching, we often use Let’s practice the grouping function of regular expressions by using regular matching IP addresses.

Assume that the IP address we want to match is between 0.0.0.0 - 255.255.255.255. We can intuitively understand that we only You need to match three digits + dot three times, and then match three digits once.

The three digits + dot mentioned here is a rule we are talking about, we can use it in the expression Convert them into rules: \d{1,3}\.. When we wrap the rules in brackets, they become groups: (\d{1,3}\ .), so the regular expression matching IP addresses can be written as: (\d{1,3}\.){3}\d{1,3}

Another way of thinking, we can also match like this: \d{1,3}(\.\d{1,3}){3}

Extension: If you are smart, you may have already It occurred to me that there is an omission in the matching of \d{1,3}. In the actual production process, \d{1,3} may match 999 kind of number, it is a wrong IP address segment. The real IP address regular matching is posted here for your reference: ((25[0-5]|2[0-4]\d|((1\ d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2}) |([1-9]?\d))) , the pleasant thing is that the grouping strategy it uses remains the same.

Backreference (reverse reference)

We consider a very special situation. When we want to match the same four IP segments, such as 100.100.100.100, the strategy of repeated matching groups fails: repeated matching of groups does not guarantee matching the same number-> ; At this time we need to use the power of the Backward Reference strategy (Young man, do you long for power? 2333, poke my avatar and take you to explore the literary and artistic path where music and code are intertwined.)

Backreferences have different syntaxes when writing regular expressions in different languages. We discuss the most common one in JS, which is a backreference in the form: \number, where number represents the serial number of the group.

Give you a simple chestnut, you can remember it instantly. If we want to match repeated three-digit numbers, we mark the rule of matching a number as a group: (\d), repeat matching the specific content of this group (the first group) three times: (\d)\1, thus achieving the goal.

We can easily distinguish repeated matching and backward reference: the former is to repeatedly match the same rule, and the latter is to match the specific content of the group.

Default Some rules need to be understood and remembered:

  • \0 represents the matching content of the entire regular expression

Regular Assertion

I still remember that I read an article called , which mentioned regular assertion.
I felt at the time It’s really easy to understand at a glance. But unfortunately, in actual production, there are too few cases of using various complex regular expressions. If I didn’t browse Baidu today, I’m afraid I wouldn’t be able to remember the classification of assertions and various usage methods.

The literal meaning of assertion is to conclude that the (program) running until this point (result) is such a "scenario". It describes a scenario, in other words, it is A "certain scenario". But what we need to remember is that our "result" is not included in the "scenario".

VueJS needs to match HTML tags, so we match: '< 'Not Fault' in ;segment>Not Fault' is an example.

If we use ordinary regular expressions, such as /<segment>.*</segment&gt ;/ will match the entire string 'Not Fault'. We use assertions and think in terms of "scenarios": '' and '' It is a "certain scenario". The result we need to match is: 'Not Fault'. No matter how the characters inside the tag change, the tag head and tag tail remain unchanged.

At one go, we Continue below

Precede assertion

Precede assertion, this is how I understand it: match the content first, and then make "scenario" assumptions.

Place it in our previous chestnut , it matches like this, and keeps matching content until it encounters the '' scenario. The syntax is as follows: (?=</segment>)

Post assertion

Later assertion, my understanding is: match the scene first, and then match the content.

Place it in our previous chestnut, first match the '' scene, and then continue to The following matching content, the syntax is as follows: (?<=<segment>)

Practice

Regular expressions match string content. So I bound the understanding of "first" and "last" to the order of content matching to facilitate understanding.

We combine the preceding assertion and the latter assertion. The entire expression is as follows: (?<=<segment>).*(?=</segment>), we can Obtained the desired result: 'Not Fault'.

"Positive and negative" assertion

In fact, what we just did was a positive scenario, and in actual situations there is also "this scenario is not satisfied" Usage scenarios.

For example, the expression we just used: (?<=<segment>).*(?=</segment>) is definitely ' ' '' scenario actually uses "positive post-assertion" and "positive first assertion" to match content. "Positive" represents a positive state.

The negative assertion is an assertion that does not satisfy the scenario... The syntax is to replace the equal sign in the positive assertion with an exclamation mark:

  • Negative look-ahead assertion(?!)

  • Negative post assertion(?<!)

For example: ['1999', '2099','2199'...'9099'] If we want to match all years ending with '99' except '1999', we can use the expression: (?<!19 )99

Related recommendations:

Commonly used basic syntax of JavaScript regular expressions

Brother Zhu talks about PHP regular expressions Where are expressions and regular expressions used?

JS password strength verification function example based on regular expression_javascript skills


The above is the detailed content of Analysis of key points of JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Mar 13, 2024 pm 06:24 PM

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Analyze whether Tencent's main programming language is Go Analyze whether Tencent's main programming language is Go Mar 27, 2024 pm 04:21 PM

Title: Is Tencent’s main programming language Go: An in-depth analysis. As China’s leading technology company, Tencent has always attracted much attention in its choice of programming languages. In recent years, some people believe that Tencent mainly adopts Go as its main programming language. This article will conduct an in-depth analysis of whether Tencent's main programming language is Go, and give specific code examples to support this view. 1. Application of Go language in Tencent Go is an open source programming language developed by Google. Its efficiency, concurrency and simplicity are loved by many developers.

Analyze the advantages and disadvantages of static positioning technology Analyze the advantages and disadvantages of static positioning technology Jan 18, 2024 am 11:16 AM

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,

Analyze and solve the reasons why Tomcat crashes Analyze and solve the reasons why Tomcat crashes Jan 13, 2024 am 10:36 AM

Tomcat crash cause analysis and solutions Introduction: With the rapid development of the Internet, more and more applications are developed and deployed on servers to provide services. As a common JavaWeb server, Tomcat has been widely used in application development. However, sometimes we may encounter problems with Tomcat crashing, which will cause the application to not run properly. This article will introduce the analysis of the causes of Tomcat crash, provide solutions, and give specific code examples.

See all articles