How do you modify part of a string through regular expressions in javascript?

demand background:

A background system of the company. The form page is paged with ajax. After clicking on the page, you want the current url to record the paging information, so that when you refresh the current page, the paging information will not be lost. For example, when I click on page 5 and record a page_num=5, in the current url to refresh the page, I go directly to page 5.

the problem now is that after clicking on paging, I need to update the paging information in url without refreshing and modifying url. Take clicking to go to page 5 as an example.

for example:

https://www.baidu.com/
url:
https://www.baidu.com/?page_num=5

https://www.baidu.com/?page_num=2
url:
https://www.baidu.com/?page_num=5

https://www.baidu.com/?page_num=2&type=test
url:
https://www.baidu.com/?page_num=5&type=test

I now hope to modify the value of page_num specifically through regular expressions, so that the rest of url will not be affected, but this code will not be written (especially regular)? Trouble God to help say the train of thought, it is best to paste some code.

an extension: the above requirement is not just a matter of modifying the page_ value specifically. If there is no page_num information in the url, how to deal with the special case of appending a page_num=5, instead of changing the page_num value? God, if you have time, please take a look at this problem and help write some code. Thank you.


you can also put parameters in hash

var hash = (location.hash || '').substring(1);
var matchPageNum = hash.match(/page_num=(\d+)/);
if (matchPageNum) {
    var matchedNum = parseInt(matchPageNum[1]);
    location.hash = hash.replace(/page_num=(\d+)/, 'page_num=' + 5);
} else {
    location.hash = hash + (hash ? '&' : '?') + 'page_num=' + 5
}

when you click on paging, location.hash = location.hash.replace (/ page_num= (\ d +) /, 'page_num=' + 5) ;


function setValue(url,key,value){
    if(url.indexOf(key)>=0){
        var regex=new RegExp("("+key+"=)\\d+","ig");
        url=url.replace(regex,function(matchStr,g1){
            return g1+value;
        });
    }else{
        if(url.indexOf("&")<0){
            url=url+"?"+key+"="+value;
        }else{
            url=url+"&"+key+"="+value;
        }
    }
    return url;
}

all you have to do is paging without refreshing the page. It's not enough to re-render the table. As for the problem of saving page numbers, local storage such as cookie,localstorage is OK, and there is no need for url to save information

. < hr >

add

you can refer to this demo http://www.qttc.net/static/de.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be8164-13e0e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be8164-13e0e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?