0

I want to know way to replace substring in url with new string.

https://pbs.twimg.com/media/DW7hPt9VAAAdKE7?format=jpg&name=small after "&name=" they are many kind of size like 900x900,medium,360x360,small

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;
    }
}

It not working I don't want to write code to check all case like

if(href.includes('900x900')
if(href.includes('medium')
if(href.includes('360x360')
if(href.includes('small')

they are way to find match at once?

huan feng
  • 7,307
  • 2
  • 32
  • 56
J. Sumittanun
  • 87
  • 1
  • 7

1 Answers1

0

change if(href.includes(/900x900|medium|360x360|small/){ to

if(href.search(/900x900|medium|360x360|small/) !== -1){ 

as includes accepts the string not the regex.

AZ_
  • 3,094
  • 1
  • 9
  • 19