Javascript gets query string values ​​using a group that does not contain a capture

Given this 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 I extract values ​​only from these parameter types 'product_cat, product_tag, attr_color, attr_size' , returning only 'mens-jeans,shirts,fall,classic-style,charcoal,brown,large,x-small ?

I tried to use a non-capture group for parameter types and a capture group only for values ​​but returning them.

 (?:product_cats=|product_tags=|attr\w+=)(\w|,|-)+ 
+5
source share
5 answers

You can get the values ​​using

 (?:product_cats|product_tags|attr\w+)=([\w,-]+) 

Remember that the character class ( [\w,-]+ ) is much more efficient than the list of alternatives ( (\w|,|-)* ), and we avoid the problem of capturing only one single character.

Here is a sample code:

 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"/> 
+2
source

You can use the following simple expression:

 /&\w+=([\w,-]+)/g 

Demo

You need to return the result of the capture group and divide them by,.

 var mystr="?cgan=1&product_cats=mens-jeans,shirts&product_tags=fall,classic-style&attr_color=charcoal,brown&attr_size=large,x-small&cnep=0 "; var myStringArray = mystr.match(/&\w+=([\w,-]+)/g); var arrayLength = myStringArray.length-1; //-1 is because of that the last match is 0 var indices = []; for (var i = 0; i < arrayLength; i++) { indices.push(myStringArray[i].split(',')); } 
+1
source

You can always use jQuery param method.

+1
source

Sort of

 /(?:product_cats|product_tag|attr_color|attr_size)=[^,]+/g 
  • (?:product_cats|product_tag|attr_color|attr_size) will match product_cats or product_tag or attr_color or attr_size)

  • = Matches value

  • [^,] A negative character class matches any other than [^,] This basically matches until the next ,

Regex Demo

Test

 string = "?cgan=1&product_cats=mens-jeans,shirts&product_tags=fall,classic-style&attr_color=charcoal,brown&attr_size=large,x-small&cnep=0"; matched = string.match(/(product_cats|product_tag|attr_color|attr_size)=[^,]+/g); for (i in matched) { console.log(matched[i].split("=")[1]); } 

will produce the result as

 mens-jeans charcoal large 
0
source

No need for regular expressions. just use split and join s.

 var s = '?cgan=1&product_cats=mens-jeans,shirts&product_tags=fall,classic-style&attr_color=charcoal,brown&attr_size=large,x-small&cnep=0'; var query = s.split('?')[1], pairs = query.split('&'), allowed = ['product_cats', 'product_tags', 'attr_color', 'attr_size'], results = []; $.each(pairs, function(i, pair) { var key_value = pair.split('='), key = key_value[0], value = key_value[1]; if (allowed.indexOf(key) > -1) { results.push(value); } }); console.log(results.join(',')); 

($. each is jQuery, but can be easily replaced if jQuery is not around)

0
source

All Articles