欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

程序员文章站 2022-03-10 18:52:32
...

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

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

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

方式一:match

返回数组

方式二

exec

实例如下:

<script>
  
 var str='<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>';
var reg=/title="(.*?)">/g;
var res = str.match(reg);
console.log(str.match(reg));
  
while( res = reg.exec(str))
{
 console.log(res[1]);
}
</script>

效果如下:

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

相关教程推荐:js教程

以上就是js中如何利用正则匹配多个全部数据的详细内容,更多请关注其它相关文章!

相关标签: js 正则