【问题标题】:How to replace url in script source如何替换脚本源中的url
【发布时间】:2016-08-05 18:21:53
【问题描述】:

有一个网址

/Kentico9/CMSPages/GetResource.ashx

在下面的脚本中,

  <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
};

我需要更改这个网址

/Kentico9/CMSPages/GetResource.ashx 

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

我尝试了以下脚本来替换,但不起作用。

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

我是如何做到这一点的?

【问题讨论】:

    标签: javascript jquery html regex scripting


    【解决方案1】:

    两件事:

    1. replace 返回新字符串。

    2. 当您将字符串作为第一个参数传递时,它只会替换 first 实例,而不是所有实例。要全部替换,您需要一个带有 g 标志的正则表达式。

    所以:

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

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

    【讨论】:

    • 3. RegExp 对象不能在引号内定义
    • @WiktorStribiżew:我相信引号中的 / 是 OP 想要替换的部分,而不是正则表达式分隔符。
    【解决方案2】:

    专门为imageURL试一试

    res.replace('"imagesUrl": "/Kentico9/', '"imagesUrl": "http://localhost/Kentico9/');
    

    【讨论】:

      【解决方案3】:

      使用这个

      var remove = "/Kentico9/CMSPages/GetResource.ashx";
      var newLink = "http://localhost/Kentico9/CMSPages/GetResource.ashx";
      $('script').each(function(){
        var link = $(this).attr('src');
        $(this).attr('src', link.replace(remove, newLink));
      })
      

      【讨论】:

        猜你喜欢
        • 2011-12-07
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 2011-02-14
        • 2019-11-18
        相关资源
        最近更新 更多