Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of greedy pattern of JavaScript regular expression

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of the greedy pattern of JavaScript regular expressions. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Greed mode:

When using a special symbol that modifies the number of matches, there are several representations that enable the same expression to match different times, such as "{mforce n}", "{m,}", "?", "*", "+", depending on the string being matched. This kind of expression with indefinite number of repeated matches always matches as many times as possible in the matching process.

Non-greedy mode:

Add a "?" after modifying the special symbol of the number of matches. Number, you can make the expressions with variable matching times match as little as possible, and make the expressions that can match but not match "mismatch" as much as possible. This matching principle is called "non-greedy" mode, also known as "reluctant" mode. If the lack of matching will lead to the failure of the whole expression matching, similar to the greedy pattern, the non-greedy pattern will match a little more to make the whole expression match successfully.

Var regex = /\ d {2pr 5} / GTX var string = "123 1234 12345 123456"; console.log (string.match (regex)); / / = > ["123", "1234", "12345", "12345"]

Where regular / d {2pr 5} / indicates that the number appears 2 to 5 times in a row. Matches 2 digits, 3 digits, 4 digits, and 5 consecutive digits. But it is greedy, and it will match as many as possible. If you can give me six, I'll take five. If you can give me three, I'll take three. Anyway, as long as it is within the limits of ability, the more the better.

Horizontal fuzzy matching

Horizontal blur means that the length of a regular matchable string is not fixed and can be in many cases. The way to achieve it is to use quantifiers. For example, {m _ r _ n}, it means that there are at least m times in a row and n times at most. For example, regular / ab {2pr 5} c / means to match a string: the first character is "a", followed by 2 to 5 characters "b", and finally the character "c".

Var regex = / ab {2 abbbc 5} c console.log string = "abc abbc abbbc abbbbc abbbbbc abbbbbbc"; console.log (string.match (regex)); / / = > ["abbc", "abbbc", "abbbbc", "abbbbbc"]

Longitudinal fuzzy matching

Vertical ambiguity refers to a regular matching string, specific to a character, it may not be a certain character, there can be a variety of possibilities. It is implemented by using character groups.

For example, [abc] indicates that the character is any one of the possible characters "a", "b", or "c". For example, / a [123] b / can match the following three strings: "A1b", "A2b", and "A3b".

Var regex = /\ d {2pr 5}? / GTX var string = "123 1234 12345 123456"; console.log (string.match (regex)); / / = > ["12", "12", "34", "12", "34", "56"] where /\ d {2 # 5}? / means that although 2 to 5 times is fine, when 2 is enough, you will not go any further.

Multi-select branch

A pattern can achieve horizontal and vertical fuzzy matching. Multi-selected branches can support one of multiple sub-modes. The specific form is as follows: (p1 | p2 | p3), where p1, p2, and p3 are submodes separated by | (pipe character) to indicate any one of them. For example, to match the strings "good" and "nice", you can use / good | nice/.

Var regex = / good | nice/g;var string = "good idea, nice try."; console.log (string.match (regex)); / / = > ["good", "nice"]

But there is a fact that we should pay attention to, for example, when I use / good | goodbye/, to match the "goodbye" string, the result is "good"

Var regex = / good | goodbye/g;var string = "goodbye"; console.log (string.match (regex)); / / = > ["good"]

Change the rule to / goodbye | the result of good/, is:

Var regex = / goodbye | good/g;var string = "goodbye"; console.log (string.match (regex)); / / = > ["goodbye"]

In other words, the branch structure is also lazy, that is, when the previous match is made, the latter one is no longer tried.

Reverse reference

For example, to write a regular support to match the following three formats

2020-02-25

2020-02-25

2020.02.25

Var regex = /\ d {4} (- |\ / |.)\ d {2} (- |\ /.)\ d {2} /; var string1 = "2020-06-12"; var string2 = "2020-06-12"; var string3 = "2020.06.12"; var string4 = "2020-02 console.log 12"; console.log (regex.test (string1)); / / trueconsole.log (regex.test (string2)); / / trueconsole.log (regex.test (string3)) / / trueconsole.log (regex.test (string4)); / / true

Among them / and. Need to escape. Although it matches the required situation, it also matches data such as "2016-06 + 12".

What if we want to require the separator to be consistent? At this point, you need to use backreferences:

Var regex = /\ d {4} (- |\ / |.)\ d {2}\ 1\ d {2} /; var string1 = "2020-06-12"; var string2 = "2020-06-12"; var string3 = "2020.06.12"; var string4 = "2020-02 string1 12"; console.log (regex.test (string1)); / / trueconsole.log (regex.test (string2)); / / trueconsole.log (regex.test (string3)) / / trueconsole.log (regex.test (string4)); / / false

Notice the 1 inside, which indicates the grouping before the reference (- | / |.). No matter what it matches (such as -), 1 matches that specific character of the same kind.

Once we know the meaning of 1, then the concepts of 2 and 3 are understood, referring to the second and third groups, respectively.

Use examples

String trim method simulation

Function trim (str) {return str.replace (/ ^\ s + |\ s scratch Universe g,'');}

Represents a hexadecimal character and can be used as a character group [0-9a-fA-F]. Among them, characters can appear 3 or 6 times, using quantifiers and branch structures. When using a branch structure, you need to pay attention to the order. The rules are as follows

Var regex = / # ([0-9a-fA-F] {6} | [0-9a-fA-F] {3}) / console.log var string = "# ffbbad # Fc01DF # FFF # ffE"; console.log (string.match (regex)); / / = > ["# ffbbad", "# Fc01DF", "# FFF", "# ffE"]

The above content is over, and the following is a supplement from other netizens for reference.

Var regExp = / 8.8 /

Var str = "8google8 8google8 8google8"

In the above example, the greedy pattern is actually used to repeat (interval) matching regular expressions are all greedy patterns.

Forward-looking mode means that a match must be followed by a string, as shown in the following example

Var regExp = / goo (? = gle)

Var strOne = google

Var strTwo = googoo

The attributive returns the match followed by the one with gle

Captive and non-captive grouping: examples are as follows:

Var regExp = / (google)\ s (2020) /

Var str = "google 2020"

What is returned is an array of [google 2020]

Var regExp = / (google)\ s (?: 2020) /

Var str = "google 2020"

Returned array [google 2020J Google]

This is the end of this article on "example Analysis of greedy patterns of JavaScript regular expressions". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report