如何匹配多个出现的子字符串 [英] How to match multiple occurrences of a substring

查看:46
本文介绍了如何匹配多个出现的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 HTML 字符串,例如:

If I have an HTML string such as:

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

我想要文字:

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*/)

但这只会返回第一个数字部分 20<span class="abc"/>

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.

推荐答案

要匹配多次使用需要使用全局选项

To match multiple times use to need use the global option

str.match(/your_expression_here/g)
                                ^

这篇关于如何匹配多个出现的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆