简体   繁体   中英

Javascript get query string values using non-capturing group

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

I tried using a non-capturing group for the param types and capturing group for just the values, but its returning both.

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

You can collect tha values using

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

Mind that a character class ( [\\w,-]+ ) is much more efficient than a list of alternatives ( (\\w|,|-)* ), and we avoid the issue of capturing just the last single character.

Here is a 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"/> 

You can use following simple regex :

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

Demo

You need to return the result of capture group and split them with , .

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(','));
}

您始终可以使用jQuery方法参数

Something like

/(?: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 an equals

  • [^,] Negated character class matches anything other than a , . Basically it matches till 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 output as

mens-jeans
charcoal
large

There is no need for regular expressions. just use split s 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 from jQuery, but can easily be replaced if jQuery is not around)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM