Javascript 替换URL中id的值
我有这种形式的URL: 例如,我想通过将9616071454替换为1 我知道,但这将替换“id”本身,而不是“id”的值Javascript 替换URL中id的值,javascript,html,string,function,url,Javascript,Html,String,Function,Url,我有这种形式的URL: 例如,我想通过将9616071454替换为1 我知道,但这将替换“id”本身,而不是“id”的值 web开发世界中有什么共同点吗?:) 只需重新替换&id=的硬代码即可 var-str='https://www.merkel.com/obama?trump=15&id=9616071454&hilarry=es'; var str2=https://www.merkel.com/obama?id=9616071454&trump=15&hilarry=es'; var
web开发世界中有什么共同点吗?:) 只需重新替换
&id=
的硬代码即可
var-str='https://www.merkel.com/obama?trump=15&id=9616071454&hilarry=es';
var str2=https://www.merkel.com/obama?id=9616071454&trump=15&hilarry=es';
var newId='123';
str=str.replace(/([&?])id=[0-9]+/,“$1id=”+newId);
str2=str2.replace(/([&?])id=[0-9]+/,'$1id='+newId);
警报(str);
警报(str2)代码>只需重新替换&id=
的硬代码
var-str='https://www.merkel.com/obama?trump=15&id=9616071454&hilarry=es';
var str2=https://www.merkel.com/obama?id=9616071454&trump=15&hilarry=es';
var newId='123';
str=str.replace(/([&?])id=[0-9]+/,“$1id=”+newId);
str2=str2.replace(/([&?])id=[0-9]+/,'$1id='+newId);
警报(str);
警报(str2)代码>它的简单模式匹配。关于模式匹配,您可以参考这篇文章
function(newValue,url) {
url=url.replace(/id=\d+/,'id='+newValue);
return url;
}
简单的模式匹配。关于模式匹配,您可以参考这篇文章
function(newValue,url) {
url=url.replace(/id=\d+/,'id='+newValue);
return url;
}
解决方案应考虑以下情况:
id
param可以包含数字以外的其他字符
- 当
id
后跟#
解决方案应考虑以下情况:
id
param可以包含数字以外的其他字符
- 当
id
后跟#
此函数起作用,允许您选择所需的方式参数
var exampleStrng ='trump=15&id=9616071454&hilarry=es'; // this is an example query string.
var urlQry = window.document.location.search.substring(1); // you can use this in live code to get the query string.
// reusable function for split in text.
function strSpliter( str, splitVal ){
return str.split(splitVal);
}
// function to reassign query parameter values.
function changQry(qry, setParam, chngVal){
var pnt = strSpliter(qry, '&'),//use the spliter function to change the query into an array split on the '&' character.
newQryArr = [], // a temp array to hold the new parameters and their value.
newQry = '',// this will be the string where the query parameters and values are reconstructed into a string.
newQryStr = '';// this will be the query with the new value.
pnt.forEach( function( item, idx ){
var param = strSpliter(item, '='); //use the spliter function to split the parameter and their value.
// checks the parameter against the one you want to change.
if( param[0] === setParam ){
param[1] = chngVal;// assigns the new value to the parameter.
newQryArr.push(param.join('=')); // rejoins the parameter and its value and pushes it into the temp array.
} else {
newQryArr.push(param.join('='));// rejoins the parameter and its value and pushes it into the temp array.
}
newQry = newQryArr.join('&');// rejoins all the parameters and their values.
newQryStr = '?' + newQry;// creates the new query string.
});
return newQryStr; // returns the new search query string.
}
changQry(exampleStrng, 'id', 77777745);
没有评论
var urlQry=window.document.location.search.substring(1)
此函数起作用,允许您选择所需的方式参数
var exampleStrng ='trump=15&id=9616071454&hilarry=es'; // this is an example query string.
var urlQry = window.document.location.search.substring(1); // you can use this in live code to get the query string.
// reusable function for split in text.
function strSpliter( str, splitVal ){
return str.split(splitVal);
}
// function to reassign query parameter values.
function changQry(qry, setParam, chngVal){
var pnt = strSpliter(qry, '&'),//use the spliter function to change the query into an array split on the '&' character.
newQryArr = [], // a temp array to hold the new parameters and their value.
newQry = '',// this will be the string where the query parameters and values are reconstructed into a string.
newQryStr = '';// this will be the query with the new value.
pnt.forEach( function( item, idx ){
var param = strSpliter(item, '='); //use the spliter function to split the parameter and their value.
// checks the parameter against the one you want to change.
if( param[0] === setParam ){
param[1] = chngVal;// assigns the new value to the parameter.
newQryArr.push(param.join('=')); // rejoins the parameter and its value and pushes it into the temp array.
} else {
newQryArr.push(param.join('='));// rejoins the parameter and its value and pushes it into the temp array.
}
newQry = newQryArr.join('&');// rejoins all the parameters and their values.
newQryStr = '?' + newQry;// creates the new query string.
});
return newQryStr; // returns the new search query string.
}
changQry(exampleStrng, 'id', 77777745);
没有评论
var urlQry=window.document.location.search.substring(1)
str.replace(/\bid=\d+/,'id=1')
我将检查@AvinashRaj,但是?:)你应该更清楚这一点。@Amit我唯一的尝试是使用replace()
,你的意思是把它包括在我的问题中吗?阿维纳什,它起作用了,谢谢!谢谢你,提图斯@gsamaras\b
匹配单词字符和非单词字符,反之亦然<代码>\d+
匹配一个或多个数字字符。因此,上面的正则表达式将匹配整个id参数及其值。str.replace(/\bid=\d+/,'id=1')
I将检查@AvinashRaj,但是?:)你应该更清楚这一点。@Amit我唯一的尝试是使用replace()
,你的意思是把它包括在我的问题中吗?阿维纳什,它起作用了,谢谢!谢谢你,提图斯@gsamaras\b
匹配单词字符和非单词字符,反之亦然<代码>\d+
匹配一个或多个数字字符。因此,上面的正则表达式将匹配整个id参数及其值。如果该id在开始时存在,该怎么办<代码>\b将覆盖所有情况。如果id在开始时存在怎么办<代码>\b将涵盖所有情况。很好的尝试。确保你检查了我的问题下面的顶部评论!;)+很好的尝试。确保你检查了我的问题下面的顶部评论!;)+1太浓了……也许我有时间的时候会读的,安德烈,谢谢!:)太浓了……也许我有时间的时候会读的,安德烈,谢谢!:)