Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match multiple occurrences of a substring

Tags:

regex

If I have an HTML string such as:

<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 do I define a regular expression to match the target sections multiple times. So far I have:

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

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

I need this to be flexible to match multiple tag / numeric sections while trimming anything leading or trailing the first / last digit in the string.

like image 607
gb2d Avatar asked Aug 12 '11 14:08

gb2d


1 Answers

To match multiple times use to need use the global option

str.match(/your_expression_here/g)
                                ^
like image 196
James Kyburz Avatar answered Oct 03 '22 22:10

James Kyburz