const
replaceUrlArg
= (
url, arg, argVal
) => {
var
pattern =
eval
(
`/([\?|\&])(
${arg}
=)([^&]*)/gi`
);
var
replaceText = arg +
'='
+ argVal;
return
url.
match
(pattern)
? url.
replace
(pattern,
`$1
${replaceText}
`
)
: url.
match
(
'[?]'
)
? url +
'&'
+ replaceText
: url +
'?'
+ replaceText;
2
、现代方式
* 替换URL的参数值
* url 目标url
* arg 需要替换的参数名称(区分大小写)
* arg_val 替换后的参数的值
* return url 参数替换后的url
const
replaceUrlArg
= (
url, arg, argVal
) => {
const
urlObj =
new
URL
(url);
urlObj.
searchParams
.
set
(arg, argVal)
return
urlObj.
href
复制代码