示例网址
的.com / projects.php&安培;?filterDate = this_week页= 5
我上面列出的查询字符串可能包含也可能没有?page=#
查询字符串。我正在寻找一种方法来获取URL(完成),搜索字符串以确定它是否具有.replace
查询字符串(也已完成),如果它不存在则添加(也已完成),但是如果它 那么用不同的数字替换它(需要帮助)。代码当前不会更改查询字符串(即page = 5不会更改为page = 6或其他任何内容)。似乎current_window_location3
方法的正则表达式不正确(请参阅下面的//Get the current URL
var current_window_location = window.location.href;
if(current_window_location.match("\\?page=([^&]+)")){
//Replace this query string
var current_window_location3 = current_window_location.replace("\\?page=([^&]+)", new_page_num);
//Go to this newly replaced location
window.location = current_window_location3;
}else{
//Add clicked '?page=#' query string to the URL
var current_window_location2 = current_window_location + "?page="+new_page_num;
//Go to this new location
window.location = current_window_location2;
}
变量)。
ListDirectory
答案 0 :(得分:0)
String.prototype.replace()
作为第一个值,搜索模式,字符串或正则表达式。如果给出了字符串("abc?"
),它将搜索要替换的文字字符串。如果正则表达式(/abc?/
)传递正则表达式匹配,则使用正则表达式进行搜索。
更改您的
var current_window_location3 = current_window_location.replace("\\?page=([^&]+)", new_page_num);
到
var current_window_location3 = current_window_location.replace(/\?page=([^&]+)/, new_page_num);
这是一个说明代码段:
var current_window_location = '.com/projects.php?&filterDate=this_week*?page=5*',
window_location = '',
new_page_num = 123;
if(current_window_location.match("\\?page=([^&]+)")){
//Replace this query string
var current_window_location = current_window_location.replace(/\?page=([^&]+)/, new_page_num);
//Go to this newly replaced location
window_location = current_window_location;
} else {
//Add clicked '?page=#' query string to the URL
var current_window_location = current_window_location + "?page="+new_page_num;
//Go to this new location
window_location = current_window_location;
}
document.write("window_location = " + window_location);