0

How do I replace all occurences of "http://localhost" in a string in javascript?

I have res=res.replace(/^http\:\/\/localhost, url);, but it does not work. How do I fix it?

url is a variable and but "localhost" is a string.

UPDATE:

With the solutions below, I still get: ReferenceError: localhost is not defined. What am I missing?

UPDATE 2:

This is the (Perl) code that inserts the JS on the page:

$form .=  qq|<script>res='$doc'; loc=document.location.href; url=loc.substring(0,loc.indexOf(":8080")); res=res.replace(/http\:\/\/localhost/g, url); document.location='data:text/html;charset:utf-8,' + res; </script>|;
kepj
  • 3
  • 1
  • 5

2 Answers2

0
res=res.replace(/http\:\/\/localhost/g, url);

'g' indicates to replace all.

Kaizhe Huang
  • 990
  • 5
  • 11
0

use /g to replace every occurrence in your string

Something like this

str = str.replace(/http:\/\/localhost/g,'');
SenthilKumarM
  • 119
  • 1
  • 8