Regex用于删除方括号内逗号分隔的数字

Regex用于删除方括号内逗号分隔的数字,regex,Regex,我正在尝试编写一个正则表达式,以从URL获取特定值 index.php?filter=3f-size[15],1f-colors[1],price[500,2000]&order=ASC 我试图从URL获取的价格值。我需要的是:5002000 我所尝试的: $.urlParam = function(name){ var results = new RegExp('[\?&]' + name + '/\[(.*?)\]/g').exec(window.location.

我正在尝试编写一个正则表达式,以从URL获取特定值

index.php?filter=3f-size[15],1f-colors[1],price[500,2000]&order=ASC
我试图从URL获取的价格值。我需要的是:5002000

我所尝试的:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '/\[(.*?)\]/g').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
}
var checkedPrice = $.urlParam('price');
alert(checkedPrice);
你可以用

\s*\[\d+(?:\s*,\s*\d+*]

详情:

  • \s*
    -零个或多个空格字符
  • \[
    -a
    [
    字符
  • \d+
    -一个或多个数字
  • (?:\s*,\s*\d+)*
    -逗号的零个或多个重复,其中包含零个或多个空格字符,然后是一个或多个数字
  • ]
    -一个
    ]
    字符
请参见JavaScript演示:

const text='index.php?filter=3f size[15],1f colors[1],price[5002000]&order=ASC\nText[1515645789],some price[5002000];
console.log(text.replace(/\s*\[\d+(?:\s*,\s*\d+*]]/g',)
/\[(\d+(?:,\d+*)\]/g
>var results = /^.*price\[([\d,]+)\].*$/.exec("index.php?filter=3f-size[15],1f-colors[1],price[500,2000]&order=ASC")
>console.log(results[1])
 500,2000
>console.log(results[1].replace(new RegExp(",", "g"), ""))
 5002000