如何在脚本源中替换url [英] How to replace url in script source

查看:92
本文介绍了如何在脚本源中替换url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个网址


/Kentico9/CMSPages/GetResource.ashx


在下面的脚本中,

 < script src =/ Kentico9 / CMSPages / GetResource.ashx?scriptfile =%7e%2fCMSScripts%2fRequireJS%2frequire.jstype =text / javascript>< / script> 
< script src =/ Kentico9 / CMSPages / GetResource.ashx?scriptfile =%7e%2fCMSScripts%2fRequireJS%2fconfig.js& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;脚本>
< script src =/ Kentico9 / CMSPages / GetResource.ashx?scriptfile =%7e%2fCMSScripts%2fcms.jstype =text / javascript>< / script>
< script type =text / javascript>
//<![CDATA [
if((window.originalPostback == null)&&(window .__ doPostBack!= null)){window.originalPostback = __doPostBack; __doPostBack = __doPostBackWithCheck; }

//]]>
< / script>
将SCRIPT SRC = / Kentico9 /的ScriptResource.axd d = _9yHV47QJb18THQ6kRwtMTYWP8AyLTDDz_ezsjVynWQhicLV_U3iBRnjAic5MX-xDgyPX48_xtLVYXhKOv2UCJKAoTTMC4wGhtJzijblJUqnor1iJ4U59KPu7436hU-U0&安培; amp; T公司= 7c776dc1 类型= 文本/ JavaScript的 >< /脚本>
将SCRIPT SRC = / Kentico9 /的ScriptResource.axd d = zf3zdXaB_cJmg3ZI845HWFeB9wwz6hDKzOk9u8r8LRzjBXOxGqGc8ov1CG1yunKlRYOyRHSZ9KBtNMB3nu1nMQXXiYklnIFMhWV0Xj3pkcNu0JnN6rQtu7_ee21y6R8Tp2tmpXsVH8ZTIabIz8lDAA2&安培; amp; T公司= 7c776dc1 类型= 文本/ JavaScript的 >< /脚本>
< script type =text / javascript>
//<![CDATA [
var CMS = CMS || {};
CMS.Application = {
isRTL:false,
isDebuggingEnabled:true,
applicationUrl:/ Kentico9 /,
imagesUrl:/Kentico9/CMSPages/GetResource.ashx?image=%5bImages.zip%5d%2f,
isDialog:false
};

我需要更改此网址

  /Kentico9/CMSPages/GetResource.ashx 

  http://localhost/Kentico9/CMSPages/GetResource.ashx 

我尝试了下面的脚本来替换,这是行不通的。

  var res =上面显示的整个html源代码; 
res.replace('/ Kentico9 /','http:// localhost / Kentico9 /');

我如何让这工作?

解决方案

两件事:


  1. 替换 返回新字符串。 当您传递一个字符串作为第一个参数时,它只会替换第一个实例,不是全部。要替换全部,您需要一个正则表达式,其中包含 g 标志。

所以:

  res = res.replace(/ \ / Kentico9 \ / CMSPages \ / GetResource\\ \\.ashx / g,'http://localhost/Kentico9/CMSPages/GetResource.ashx'); 
// ^ - 赋值^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
//正则表达式全局

请注意 / 在正则表达式中被转义,否则它们在正则表达式中有特殊含义。


There is a url

/Kentico9/CMSPages/GetResource.ashx

in the following script,

  <script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fRequireJS%2frequire.js" type="text/javascript"></script>
<script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fRequireJS%2fconfig.js&amp;resolvemacros=1" type="text/javascript"></script>
<script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fcms.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
if ((window.originalPostback == null) && (window.__doPostBack != null)) { window.originalPostback = __doPostBack; __doPostBack = __doPostBackWithCheck; }

//]]>
</script>
<script src="/Kentico9/ScriptResource.axd?d=_9yHV47QJb18THQ6kRwtMTYWP8AyLTDDz_ezsjVynWQhicLV_U3iBRnjAic5MX-xDgyPX48_xtLVYXhKOv2UCJKAoTTMC4wGhtJzijblJUqnor1iJ4U59KPu7436hU-u0&amp;t=7c776dc1" type="text/javascript"></script>
<script src="/Kentico9/ScriptResource.axd?d=zf3zdXaB_cJmg3ZI845HWFeB9wwz6hDKzOk9u8r8LRzjBXOxGqGc8ov1CG1yunKlRYOyRHSZ9KBtNMB3nu1nMQXXiYklnIFMhWV0Xj3pkcNu0JnN6rQtu7_ee21y6R8Tp2tmpXsVH8ZTIabIz8lDAA2&amp;t=7c776dc1" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var CMS = CMS || {};
CMS.Application = {
"isRTL": "false",
"isDebuggingEnabled": true,
"applicationUrl": "/Kentico9/",
"imagesUrl": "/Kentico9/CMSPages/GetResource.ashx?image=%5bImages.zip%5d%2f",
"isDialog": false
};

I need to change this url

/Kentico9/CMSPages/GetResource.ashx 

to

http://localhost/Kentico9/CMSPages/GetResource.ashx

I tried the following script to replace ,which is not working.

var res = "entire html source shown above";
res.replace('/Kentico9/', 'http://localhost/Kentico9/');

How i make this working?

解决方案

Two things:

  1. replace returns the new string.

  2. When you pass a string as the first argument, it only replaces the first instance, not all of them. to replace all, you need a regular expression with the g flag.

So:

res = res.replace(/\/Kentico9\/CMSPages\/GetResource\.ashx/g, 'http://localhost/Kentico9/CMSPages/GetResource.ashx');
//  ^-- assign    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
//                regex                                    global

Note that the / and the . in the regular expression are escaped, as otherwise they have special meaning in the regex.

这篇关于如何在脚本源中替换url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆