Home  >  Article  >  Web Front-end  >  How to use regular expressions to match multiple all data in js

How to use regular expressions to match multiple all data in js

王林
王林forward
2020-04-03 09:09:033254browse

How to use regular expressions to match multiple all data in js

What if we need to get all the content in the title?

Add g after the regular expression to indicate multiple matches

Method 1: match

Return array

Method 2

exec

The example is as follows:

<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>

The effect is as follows:

How to use regular expressions to match multiple all data in js

Recommended related tutorials: js tutorial

The above is the detailed content of How to use regular expressions to match multiple all data in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete