我想知道用新字符串替换url中的子字符串的方法。
https://pbs.twimg.com/media/DW7hPt9VAAAdKE7?format=jpg&name=small 在"& name ="它们有很多种类 900x900,中,360x360,小
let href = document.location.href;
if(!href.includes('&name=orig')){
if(href.includes(/900x900|medium|360x360|small/){ //if href have some size in regular expression
// I try to make it search for substring in regular expression
document.location.href = href.replace(/900x900|medium|360x360|small/,'orig');
}
else{ //if url don't have '&name=' like above
var adding = '&name=orig';
document.location.href = link+adding;
}
}
它不起作用 我不想编写代码来检查所有案例,如
if(href.includes('900x900')
if(href.includes('medium')
if(href.includes('360x360')
if(href.includes('small')
他们是一次找到比赛的方法吗?
答案 0 :(得分:0)
更改if(href.includes(/ 900x900 | medium | 360x360 | small /){to
if(href.search(/900x900|medium|360x360|small/) !== -1){
as includes接受字符串而不是正则表达式。