更新URL参数与javascript/jquery与regEx

Update URL parameter with javascript/jquery with a regEx

本文关键字:jquery regEx javascript 参数 更新 URL      更新时间:2023-09-26

我有一个web, URL是这样的:

http://www.example.com/index.php?mod=7&证交会= franquicias&月= 10,2013年=

里面有一个日期选择器;它应该改变get参数月和年;然后重新加载页面。

在HTML中,我在负责减少日期的按钮中做了以下操作:

<button type="button" class="btnIcon previous lbutton" onclick="changeMonth('previous',{$smarty.get.month},{$smarty.get.year})">

管理它的javascript函数是

    function changeMonth(changeType, month, year){
    changeMonth;
    switch (changeType){
    case "previous":
        changeMonth = month-1;
        if (changeMonth <= 0) changeMonth = 12;
        if (changeMonth < 10) changeMonth = "0"+changeMonth;
        window.location = updateURLParameter($(location).attr('href'), month, changeMonth);
        break;
    case "next":
        break;
    }       
}
function updateURLParameter(url, param, paramVal){
    var regEx = /month=[0-9]{3}/;
    console.log("paramVal: "+paramVal);
            // paramVal here is ever correct.
    var newUrl = url.replace(regEx, 'month='+paramVal);
    console.log(newUrl);
    return newUrl;
}

它似乎应该工作,现在奇怪的是,只有当url中的GET参数是&month=03,当有其他月份,url时,它才工作。Replace不能正确地替换url。重要的是要注意'paramVal'在函数'updateUrlParameter'它总是正确的;所以问题是在urlReplace从'function updateUrlParameter'。

谢谢你的帮助。

我不知道它是如何工作的,即使是month=03[0-9]{3}正好匹配3位数字,而03不匹配。正确的regexp是:

/'bmonth='d{2}'b/