Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

regex - How to match multiple occurrences of a substring

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To match multiple times use to need use the global option

str.match(/your_expression_here/g)
                                ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...