如何在JavaScript中的字符串中替换所有出现的“http://localhost”?我有res=res.replace(/^http\:\/\/localhost, url);
,但它不起作用。我如何解决它?全部替换为javascript
url
是一个变量,但“localhost”是一个字符串。
UPDATE:
与下面的解决方案,我仍然得到:ReferenceError: localhost is not defined
。我错过了什么?
更新2:
这是(Perl的)代码插入JS在网页:
$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>|;
声明未完成。 – Dere0405
@ Dere0405我相信我现在完成了。 – kepj
那么,如果你使用下面的解决方案得到这个结果,那么你就错了。 – epascarello