regex - Javascript get query string values using non-capturing group -
given query string:
?cgan=1&product_cats=mens-jeans,shirts&product_tags=fall,classic-style&attr_color=charcoal,brown&attr_size=large,x-small&cnep=0
how can extract values these param types 'product_cat, product_tag, attr_color, attr_size'
returning 'mens-jeans,shirts,fall,classic-style,charcoal,brown,large,x-small
?
i tried using non-capturing group param types , capturing group values, returning both.
(?:product_cats=|product_tags=|attr\w+=)(\w|,|-)+
you can collect tha values using
(?:product_cats|product_tags|attr\w+)=([\w,-]+)
mind character class ([\w,-]+
) more efficient list of alternatives ((\w|,|-)*
), , avoid issue of capturing last single character.
here code sample:
var re = /(?:product_cats|product_tags|attr\w+)=([\w,-]+)/g; var str = '?cgan=1&product_cats=mens-jeans,shirts&product_tags=fall,classic-style&attr_color=charcoal,brown&attr_size=large,x-small&cnep=0'; var res = []; while ((m = re.exec(str)) !== null) { res.push(m[1]); } document.getelementbyid("res").innerhtml = res.join(",");
<div id="res"/>
Comments
Post a Comment