Javascript - 如果 URL 字符串的最后一个字符是“+”,则将其删除...如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6253567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:57:35  来源:igfitidea点击:

Javascript - If Last Character of a URL string is "+" then remove it...How?

javascriptstring

提问by Zach Nicodemous

This is a continuation from an existing question. Javascript - Goto URL based on Drop Down Selections (continued!)

这是现有问题的延续。 Javascript - 基于下拉选择的转到 URL(续!)

I am using dropdown selects to allow my users to build a URL and then hit "Go" to goto it.

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

Is there any way to add an additional function that checks the URL before going to it?

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

My URLs sometimes include the "+" character which I need to remove if its the last character in a URL. So it basically needs to be "if last character is +, remove it"

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

This is my code:

这是我的代码:

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

回答by Matt Ball

var url = /* whatever */;

url = url.replace(/\+$/, '');

For example,

例如,

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

回答by Jon

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

回答by Black Mamba

Found another solution using str.endsWith("str")

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

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

回答by dpmattingly

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

回答by Mfoo

$(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();
    });
});

Just seems easier.

只是看起来更容易。

回答by givemesnacks

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