Javascript使用非捕获组获取查询字符串值

时间:2022-12-06 17:15:54

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?

如何仅从这些参数类型'product_cat,product_tag,attr_color,attr_size'中提取值,仅返回'男装牛仔裤,衬衫,秋季,经典款式,木炭色,棕色,大号,x小码?

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

我尝试将非捕获组用于param类型并仅为值捕获组,但它返回两者。

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

5 个解决方案

#1


2  

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.

请注意,字符类([\ w, - ] +)比替代列表((\ w |,| - )*)更有效,并且我们避免捕获最后一个单个字符的问题。

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

#2


1  

You can always use a jQuery method param.

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

#3


1  

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

#4


0  

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)

    (?:product_cats | product_tag | attr_color | attr_size)将匹配product_cats或product_tag或attr_color或attr_size)

  • = Matches an equals

    =匹配等于

  • [^,] Negated character class matches anything other than a ,. Basically it matches till the next ,
  • [^,]否定字符类匹配除a之外的任何内容。基本上它匹配到下一个,

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

#5


0  

There is no need for regular expressions. just use splits and joins.

不需要正则表达式。只需使用拆分和连接。

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)

($ .each来自jQuery,但如果jQuery不在,可以轻松替换)

#1


2  

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.

请注意,字符类([\ w, - ] +)比替代列表((\ w |,| - )*)更有效,并且我们避免捕获最后一个单个字符的问题。

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

#2


1  

You can always use a jQuery method param.

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

#3


1  

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

#4


0  

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)

    (?:product_cats | product_tag | attr_color | attr_size)将匹配product_cats或product_tag或attr_color或attr_size)

  • = Matches an equals

    =匹配等于

  • [^,] Negated character class matches anything other than a ,. Basically it matches till the next ,
  • [^,]否定字符类匹配除a之外的任何内容。基本上它匹配到下一个,

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

#5


0  

There is no need for regular expressions. just use splits and joins.

不需要正则表达式。只需使用拆分和连接。

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)

($ .each来自jQuery,但如果jQuery不在,可以轻松替换)