js 将一个网页链接去除只留下参数
可以使用JavaScript的正则表达式来去除网页链接中的非参数部分。
function removeUrlLink(url) {
// 匹配链接中的参数部分
const regex = /\?([^#]+)/;
const match = regex.exec(url);
// 如果匹配到参数部分,则返回参数部分,否则返回空字符串
return match ? match[1] : '';
}
const url = 'https://example.com/page?param1=value1¶m2=value2#anchor';
const params = removeUrlLink(url);
console.log(params); // 输出:param1=value1¶m2=value2
上述代码中,removeUrlLink函数使用正则表达式来匹配链接中的参数部分(即问号后面的部分,但不包括井号后面的部分),然后返回匹配到的参数部分。如果链接中没有参数部分,则返回空字符串。
原文地址: https://www.cveoy.top/t/topic/izLN 著作权归作者所有。请勿转载和采集!