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.
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 .