-1

I am trying to do a case insensitive replace based on a URL where I need to include forward slashes in the text to be replaced. I know that you can remove case sensitivity like this:

str.replace(/lorem/gi, 'ipsum');

But how would I do that if the text to replace contained a forward slash such as this:

str = 'http://example.com/lorem/ipsum.txt';

and I want to replace /lorem/ with /ipsum/

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
user13286
  • 3,027
  • 9
  • 45
  • 100

1 Answers1

2

You just have to escape the forward slashes. The character for that is the backslash \

let str = 'http://example.com/lorem/ipsum.txt';
let final = str.replace(/\/lorem\//gi, '/ipsum/');

console.log(final);
Chris Strickland
  • 3,388
  • 1
  • 16
  • 18