56

How to match multiple occurrences of a substring

If I have an HTML string, for example:

<div><p>Β£20<span class="abc" /><span class="def">56</span></p></div> 

And I want the text:

 20<span class="abc" /><span class="def">56 

How to define a regular expression to match sections of the target multiple times . So far, I:

 str.match(/\d*<[^>]*>\d*/) 

But this will only return the first section number 20<span class="abc" />

I need this to be flexible in order to match multiple sections of tags / numbers when trimming anything leading or ending the first / last digit in a string.

+11
source share
3 answers

To match multiple times, use the global option as necessary

 str.match(/your_expression_here/g) ^ 
+6
source

Adding /g not enough if you want to match multiple occurrences of a substring. In this case, reluctant register quantifiers can be used as described here.

Given the line:

 <div><p>Β£20<span class="abc" /><span class="def">56</span></p></div> 

You will get the text you want to use:

 \d+.*>\d+ 

But given the fact that the same line is repeated twice:

 <div><p>Β£20<span class="abc" /><span class="def">56</span></p></div><div><p>Β£20<span class="abc" /><span class="def">56</span></p></div> 

You will not find the target selection several times. You will find it only once because of its greedy nature .* . To make .* Non-greedy or reluctant, just add ? after * , and you will receive:

 \d+.*?>\d+ 

Which will find both occurrences of the requested substring as shown here .

+2
source

Just let the group repeat: (?:...)+ means "Match ... 1 or more times:

 str.match(/\d+(?:<[^>]*>)+\d+/) 

As suggested by Alan Moore, I also changed \d* to \d+ , making the required numbers instead of optional.

0
source

Source: https://habr.com/ru/post/894917/


All Articles