javascriptgreasemonkeytampermonkeygreasemonkey-4

Greasemonkey: How to remove random url parts?


I need to change images like this from...

https://www.website.com/images/stuff/95/jgfij/public/2019/09/cow.gif https://www.website.com/images/stuff/df3/4gy0/public/2015/03/horse.png https://www.website.com/images/stuff/odpk/f049/public/2020/08/dog.jpg

into...

https://www.website.com/images/stuff/public/2019/09/cow.gif https://www.website.com/images/stuff/public/2015/03/horse.png https://www.website.com/images/stuff/public/2020/08/dog.jpg

I tried this but it doesn't work

document.body.innerHTML = document.body.innerHTML.replace('https://www.website.com/images/stuff/*/public/,'https://www.website.com/images/stuff/public/');

I've been trying to find something that remove everything inbetween a certain part of the url and I've never had any luck.


Solution

  • Update:

    Your example probably wasn't quite what you wanted. Maybe the following:

    blahblahblah.replace(new RegExp('https://www.website.com/images/stuff/.*/public/'),
        'https://www.website.com/images/stuff/public/');
    

    You were missing an endquote on the regex, and had /*, which means "zero or more slashes" when you probably want .*, "zero or more of any character".

    I also discovered that you have to pass in the pattern as a regex, not a string. See comment below.