【问题标题】:How can I cut a substring from a string to the end using Javascript?如何使用Javascript将子字符串从字符串剪切到末尾?
【发布时间】:2012-03-13 20:12:20
【问题描述】:

我有一个网址:

http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe

我想使用 javascript 获取最后一个破折号后的地址:

dashboard.php?page_id=projeto_lista&lista_tipo=equipe

【问题讨论】:

  • 您好 Erick,您共享的链接在您的本地计算机上,因此不可见。如果你用一个简单的代码 sn-p 来重新表述你的问题,那么我可以尝试回答
  • 您的子字符串是否总是以/dashboard... 开头?编辑:@Rowan,该链接只是他试图从中删除子字符串的字符串的一个示例。我希望。
  • 哈哈,哎呀。失败 :) 听@Elliot - 当它显示为链接时让我很困惑,自然反应是点击
  • 你好。谢谢你的回复。服务器没有区别。我想在最后一个“/”之后剪切。
  • JavaScript substr 可以做到这一点,不需要 jQuery。

标签: javascript string url


【解决方案1】:

您可以使用indexOfsubstr 来获取您想要的子字符串:

//using a string variable set to the URL you want to pull info from
//this could be set to `window.location.href` instead to get the current URL
var strIn  = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe',

    //get the index of the start of the part of the URL we want to keep
    index  = strIn.indexOf('/dashboard.php'),

    //then get everything after the found index
    strOut = strIn.substr(index);

strOut 变量现在保存/dashboard.php 之后的所有内容(包括该字符串)。

这是一个演示:http://jsfiddle.net/DupwQ/

更新:

上面示例中的strOut 变量包含前缀正斜杠,并且要求输出不应。

strOut = strIn.substr(index) 替换为strOut = strIn.substr(index + 1) 通过在字符串中更前一个字符开始子字符串来修复此特定用例的输出。

您可以做的其他事情是在特定搜索词(不包括在内)之后搜索字符串:

var strIn = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
var searchTerm = '/dashboard.php?';
var searchIndex = strIn.indexOf(searchTerm);
var strOut = strIn.substr(searchIndex + searchTerm.length); //this is where the magic happens :)

strOut 现在包含/dashboard.php? 之后的所有内容(不包括在内)。

这是一个更新的演示:http://jsfiddle.net/7ud0pnmr/1/

文档 -

【讨论】:

  • 我看到很多人投票,但这有两个问题:1)这包括/ char,但在问题中,参考没有/。 2)为了解决这个问题,如果使用 indexOf...+1 则在没有任何 /dashboard... 时返回所有字符串,所以我建议使用此解决方案:stackoverflow.com/a/58456746/1407491
  • @NabiK.A.Z.接得好。已更新答案以反映输出中包含的斜线。
【解决方案2】:

这可能是新的,但substring 方法会返回从指定索引到字符串末尾的所有内容。

var string = "This is a test";

console.log(string.substring(5));
// returns "is a test"

【讨论】:

    【解决方案3】:

    如果开头总是“http://localhost/40ATV”,你可以这样做:

    var a = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
    var cut = a.substr(22);
    

    【讨论】:

    • 有时可能有助于解释某些事情是如何工作的,而不是仅仅用实用的答案来回答问题。 substr/substring 和 indexOf 都是可以/应该涵盖的重要操作。
    • 感谢您的意见。我是新来的,所以请和我一起裸露:)
    • 没问题.. 只是为更有建设性的答案提供建议。 :)
    【解决方案4】:

    原生 JavaScript 字符串方法 substr[MDN] 可以完成你所需要的。只需提供起始索引并省略长度参数,它就会一直抓取到最后。

    现在,如何获取起始索引?你没有给出任何标准,所以我对此无能为力。

    【讨论】:

    • 我觉得没那么容易。 substr 找不到特定的字符组。
    • 是的。为此,您需要找到起始索引。
    【解决方案5】:

    不需要 jQuery,普通的旧 javascript 就可以完成这项工作。

    var myString = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
    var mySplitResult = myString.split("\/");
    document.write(mySplitResult[mySplitResult.length - 1]);​
    

    如果你想要前导 /

    document.write("/" + mySplitResult[mySplitResult.length - 1]);​
    

    【讨论】:

      【解决方案6】:

      首先SPLIT网址:

      var str = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
      var arr_split = str.split("/");
      

      找到最后一个数组:

      var num = arr_split.length-1;
      

      你在最后一个破折号后得到地址:

      alert(arr_split[num]);
      

      【讨论】:

        【解决方案7】:

        你可以使用这个代码,如果没有dashboard...返回空。

        var str = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
        var index = str.indexOf('/dashboard.php') + 1;
        var result = '';
        if (index != 0) {
          result = str.substr(index);
        }
        console.log(result);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-05
          • 1970-01-01
          相关资源
          最近更新 更多