首页 > web前端 > js教程 > 正文

js中如何利用正则匹配多个全部数据

王林
发布: 2020-04-03 09:09:03
转载
3196 人浏览过

js中如何利用正则匹配多个全部数据

如果我们需要获取所有title里的内容该如何做呢?

正则表达式后面加g表示多次匹配

方式一:match 

返回数组

方式二

exec

实例如下:

<script>
  
 var str=&#39;<a href="//www.aliexpress.com/store/product/OOOT-BAORJCT-174296-22mm-10yard-lot-cartoon-Ribbons-Thermal-transfer-Printed-grosgrain-Wedding-Accessories-DIY-handmade/2349150_32810795709.html" rel="external nofollow" rel="external nofollow" title="OOOT BAORJCT 174296 22mm 10yard/lot cartoon Ribbons Thermal transfer Printed grosgrain Wedding Accessories DIY handmade material">OOOT BAORJCT 174296 22mm 10yard/lot cartoon Ribbons...</a><a href="//www.aliexpress.com/store/product/OOOT-BAORJCT-174296-22mm-10yard-lot-cartoon-Ribbons-Thermal-transfer-Printed-grosgrain-Wedding-Accessories-DIY-handmade/2349150_32810795709.html" rel="external nofollow" rel="external nofollow" title="999OOOT BAORJCT 174296 22mm 10yard/lot cartoon Ribbons Thermal transfer Printed grosgrain Wedding Accessories DIY handmade material">OOOT BAORJCT 174296 22mm 10yard/lot cartoon Ribbons...</a>&#39;;
var reg=/title="(.*?)">/g;
var res = str.match(reg);
console.log(str.match(reg));
  
while( res = reg.exec(str))
{
 console.log(res[1]);
}
</script>
登录后复制

效果如下:

f7d764f2f2b93a7feb85e596aa86993.png

相关教程推荐:js教程

以上是js中如何利用正则匹配多个全部数据的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:jb51.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!