【问题标题】:How to replace duplicated 'slash' into a single slash from a string?如何将重复的“斜杠”替换为字符串中的单个斜杠?
【发布时间】:2013-06-20 20:02:03
【问题描述】:

如何替换字符串中重复的“斜线”?

例如,

str = '/estate//info//';  
alert(fragment.replace(/\/\/+/, "/"));

结果,

/estate/info//

但我在追求,

/estate/info/

【问题讨论】:

    标签: javascript jquery regex replace


    【解决方案1】:

    试试这个:

    str = '/estate//info//';  
    alert(str.replace(/\/\/+/g, "/"));  
    // where 'g' will do the global search and replace it with single '/'
    

    【讨论】:

      【解决方案2】:

      试试这个,

      str = '/estate//info//';  
      alert(fragment.replaceAll("//", "/"));
      

      【讨论】:

      • 将提醒/estate/info//
      【解决方案3】:

      你也可以试试

        var val = "\\val1\\val2\\val3";
        val = val.substr(0, val.lastIndexOf("\\"));
        alert(val);
      

      Fiddle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 2019-09-18
        • 2023-03-16
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 2018-06-07
        相关资源
        最近更新 更多