正则表达式删除方括号内的逗号分隔数字 [英] Regexfor removal of comma separated numbers inside square brackets

查看:56
本文介绍了正则表达式删除方括号内的逗号分隔数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a regex to get specific value from the URL.

index.php?filter=3f-size[15],1f-colors[1],price[500,2000]&order=ASC

我试图从 URL 获取价格值的内容.我需要得到的是:500,2000

what I am trying to get price value from the URL. What I need to get is : 500,2000

我尝试过的:

$.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);

推荐答案

>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

这篇关于正则表达式删除方括号内的逗号分隔数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆