如何用JavaScript正则表达式匹配所有长尾词数据?

2026-04-05 19:201阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计388个文字,预计阅读时间需要2分钟。

如何用JavaScript正则表达式匹配所有长尾词数据?

要求:获取所有title中的内容,使用正则表达式匹配后,添加g标志表示多次匹配,并返回匹配结果。

代码:javascriptvar str='a href=/product/FOOT-BAORJCT-174296-22mm-10yard-lot-cartoon-Ribbons';var regex=/href=(.*?)/g;var matches=regex.exec(str);

while (matches) { console.log(matches[1]); matches=regex.exec(str);}

需求:获取所有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>

效果

如何用JavaScript正则表达式匹配所有长尾词数据?

总结

以上所述是小编给大家介绍的js正则匹配多个全部数据问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易盾网络网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

本文共计388个文字,预计阅读时间需要2分钟。

如何用JavaScript正则表达式匹配所有长尾词数据?

要求:获取所有title中的内容,使用正则表达式匹配后,添加g标志表示多次匹配,并返回匹配结果。

代码:javascriptvar str='a href=/product/FOOT-BAORJCT-174296-22mm-10yard-lot-cartoon-Ribbons';var regex=/href=(.*?)/g;var matches=regex.exec(str);

while (matches) { console.log(matches[1]); matches=regex.exec(str);}

需求:获取所有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>

效果

如何用JavaScript正则表达式匹配所有长尾词数据?

总结

以上所述是小编给大家介绍的js正则匹配多个全部数据问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易盾网络网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!