【问题标题】:Javascript - If Last Character of a URL string is "+" then remove it...How?Javascript - 如果 URL 字符串的最后一个字符是“+”然后删除它...如何?
【发布时间】:2011-09-09 08:40:00
【问题描述】:

这是现有问题的延续。 Javascript - Goto URL based on Drop Down Selections (continued!)

我正在使用下拉选择来允许我的用户构建一个 URL,然后点击“Go”转到它。

有没有办法添加一个额外的功能,在去之前检查 URL?

我的 URL 有时包含“+”字符,如果它是 URL 中的最后一个字符,我需要将其删除。 所以它基本上需要是“如果最后一个字符是+,则删除它”

这是我的代码:

$(window).load(function(){
    $('form').submit(function(e){
        window.location.href = 
            $('#dd0').val() +
            $('#dd1').val()+
            $('#dd2').val()+
            $('#dd3').val();
        e.preventDefault();
    });
});

【问题讨论】:

标签: javascript string


【解决方案1】:

使用str.endsWith("str")找到另一个解决方案

var str = "Hello this is test+";
if(str.endsWith("+")) {
  str = str.slice(0,-1);
  console.log(str)
}
else {
  console.log(str);
}

Matt Ball 的 Replace 方法看起来也不错。我已经更新它以处理末尾有多个 + 的情况。

let str = "hello+++++++++";
str = str.replace(/\++$/, '');
console.log(str);

【讨论】:

    【解决方案2】:

    function removeLastPlus(str) {
      if (str.slice(-1) == '+') {
        return str.slice(0, -1);  
      }
      return str;
    }

    【讨论】:

      【解决方案3】:
      $(window).load(function(){
          $('form').submit(function(e){
              var newUrl = $('#dd0').val() +
                  $('#dd1').val()+
                  $('#dd2').val()+
                  $('#dd3').val();
              newUrl = newUrl.replace(/\+$/, '');
              window.location.href = newUrl;
              e.preventDefault();
          });
      });
      

      只是看起来更容易。

      【讨论】:

        【解决方案4】:
        function removeLastPlus (myUrl)
        {
            if (myUrl.substring(myUrl.length-1) == "+")
            {
                myUrl = myUrl.substring(0, myUrl.length-1);
            }
        
            return myUrl;
        }
        
        $(window).load(function(){
            $('form').submit(function(e){
                var newUrl = $('#dd0').val() +
                    $('#dd1').val()+
                    $('#dd2').val()+
                    $('#dd3').val();
                newUrl = removeLastPlus(newUrl);
                window.location.href = newUrl;
                e.preventDefault();
            });
        });
        

        【讨论】:

        • 如果 URL 以多个加号结尾,比如连续三个?
        • @maerics OP 似乎没有询问这个案例。
        • 只是扮演魔鬼的拥护者,从阅读 OP 的代码中我想到“dd[0-3]”只能包含加号,但我想我只是很难 =)
        • 我不完全确定如何将此代码与我的代码合并。有可能吗?
        • 您是要检查每一个还是一起检查所有这些? $('#dd0').val() + $('#dd1').val()+ $('#dd2').val()+ $('#dd3').val()
        【解决方案5】:
        <script type="text/javascript">
          function truncate_plus(input_string) {
            if(input_string.substr(input_string.length - 1, 1) == '+') {
              return input_string.substr(0, input_string.length - 1);
            }
            else
            {
              return input_string;
            }
          }
        </script>
        

        【讨论】:

        • 为什么使用substr() 而不是charAt() 来检索字符串的最后一个字符?
        【解决方案6】:
        var url = /* whatever */;
        
        url = url.replace(/\+$/, '');
        

        例如,

        > 'foobar+'.replace(/\+$/, '');
          "foobar"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-24
          • 1970-01-01
          • 2015-04-01
          • 2020-03-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多