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

How to capture groups in regular expressions

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

Share

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

Xiaobian to share with you how to capture groups in regular expressions, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

1: () capture group

/ go+/

The above regular expression indicates that a letter g is followed by one or more letters o, which can match go or goooo. But what if we want to apply + not just to the letter o, but to go as a whole? The way to do this is to give go parentheses:

/ (go) + /

In order to match globally and regardless of case, we will next add ig to our regular, these two flag:

Let reg = / (go) + / ig;'go is g gogo'.match (reg); / / ["go", "gogo"]

In the above example (go), a capture group (capturing group) is formed. Next, take a look at an example of using a capture group to deepen your understanding of it:

Let reg = / (\ d {2}). (\ d {2}). (\ d {4}) /; let originString = '10.25.2017 prepare reg.test (originString); / / trueRegExp.$1; / / 10RegExp.account2; / / 25RegExp.room2; / / 2017

In the above example, we have three sets of parentheses to form three capture groups, and the regular expression (our RegExp in javaScript) caches the string matched by the capture group, denoted by $n, and n represents the capture group.

Suppose we have a requirement now: change the display format from 10.25.2017 to 2017-10-25.

We know that String's replace () method is often used with regular expressions. In the replace () method, we can directly use the results of the capture group:

Let reg = / (\ d {2}). (\ d {2}). (\ d {4}) /; let originString = '10.25.2017piano newString = originString.replace (reg,' $3muri / originString.replace'); console.log (newString); / / "2017-10-25"

2: (?:) non capturing group non-capture packet

Sometimes we may just want to match the grouping, but do not want to cache (do not want to capture) the matching results, so we can add?: in front of our grouping mode. For example, in the time example above, we don't want to capture the result of the first grouping, so we can do this:

Let reg = / (?:\ d {2}). (\ d {2}). (\ d {4}) /; let originString = '10.25.2017 groups reg.test (originString); / / trueRegExp.$1; / / 25RegExp.2017; / / 2017originString.match (reg); / / ["10.25.2017", "25", "2017", index: 0, input: "10.25.2017", groups: undefined]

As you can see from the above example, our regular expression still matches (the result of test () is still true), but RegExp.$1 is not the number 10, but 25, because we put?:, 10 in the first parenthesis and it won't be captured. The execution result of match () is also affected by?:: there is no longer '10' in the result of match ().

3: (?) positive lookahead forward-looking capture

There is a sentence: 1 apple costs 10 minutes. We want to play Standard PvP match? The previous price (here is a number), but note that it does not match the number 1 at the beginning of the sentence. In this case, forward-looking capture can be used:

Let reg = /\ d + (? =?) / gposit reg1 = /\ dlemetta str ='1 apple costs 10 str.match str.match (reg1); / / [1 "," 10 "]

In the above example, reg1 only needs to match the number, and there is no requirement for what the number is followed by, so it can match to 1 ~ 10. But reg uses proactive matching, so it can only match to 10.

Maybe you can already understand what forward-looking capture is from the above comparison, which means:

/ x (? = y) / matches x, but only if [after] [is] y of x

4: (?) Negative lookahead negative forward-looking capture

We have learned what positive forward-looking matching is, and we can also guess from the literal meaning that negative forward-looking capture is:

/ x (?! y) / matches x, but only in the case of [after] [not] y

For example, in the following example, we want to match the number 1 instead of? The front 2 can be used?!:

Let reg = /\ d + (?!) / g str str ='1 apple costs 2 match (reg); ['1']

5:?

/ (? 7: (? =), (?), (?)

By default, the above four forward and backward views do not match the contents of the capture group by default, that is, do not match the conditions in parentheses. For example, our forward-looking / d + (? =?) / g will only match numbers, not? If we want to match? What should I do? The answer is yes? Also wrap a parenthesis:

Let str = "1 turkey costs 2?"; let reg = /\ d + (?) /; str.match (reg); / / ["2", "?", index: 15, input: "1 turkey costs 2?", groups: undefined]

This matches the number 2 and the one behind it.

Let's take a look at the rear-care type:

Let str = "1 turkey costs $2"; let reg = / (?

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