1

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.

1 Answers1

0

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.

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
  • I appreciate the help but it didn't work. Would it matter if I used (.*) or [.*]? – justaguy48437 May 09 '20 at 01:58
  • No, sorry, the problem is that String.replace(), when you pass in the first value as a String, it does a literal string replacement, not a regex replacement. You have to pass in the first value as a regex object. Just a sec, I'll update my answer. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – xdhmoore May 09 '20 at 02:04
  • Okie dokie. Try now., – xdhmoore May 09 '20 at 02:05
  • Still doesn't work. It works when I do this `document.body.innerHTML = document.body.innerHTML.replace('https://www.website.com/f/styles/84ht879ht478/public/all/', 'https://www.website.com/f/all/');` but I'd have to do a bunch of them – justaguy48437 May 09 '20 at 02:12
  • You have to use a RegExp() or a /blahblahblah/ regular expression literal, not a string as your first argument. What does console.log(document.body.innerHTML) show you? – xdhmoore May 09 '20 at 02:14