Regex用于删除方括号内逗号分隔的数字
我正在尝试编写一个正则表达式,以从URL获取特定值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.
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+)*
-一个]
字符]
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