繁体   English   中英

返回不带斜杠的字符串

[英]Return string without trailing slash

我有两个变量:

site1 = "www.somesite.com";  
site2 = "www.somesite.com/";  

我想做这样的事情

function someFunction(site)
{
    // If the var has a trailing slash (like site2), 
    // remove it and return the site without the trailing slash
    return no_trailing_slash_url;
}

我该怎么做呢?

尝试这个:

function someFunction(site)     
{     
    return site.replace(/\/$/, "");
} 
function stripTrailingSlash(str) {
    if(str.substr(-1) === '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
}

注意:IE8 和更早版本不支持负 substr 偏移。 如果您需要支持那些古老的浏览器,请改用str.length - 1

ES6 / ES2015 提供了一个 API 用于询问字符串是否以某些内容结尾,这使得编写更清晰、更易读的 function 成为可能。

const stripTrailingSlash = (str) => {
    return str.endsWith('/') ?
        str.slice(0, -1) :
        str;
};

我会使用正则表达式:

function someFunction(site)
{
// if site has an end slash (like: www.example.com/),
// then remove it and return the site without the end slash
return site.replace(/\/$/, '') // Match a forward slash / at the end of the string ($)
}

不过,您需要确保变量site是一个字符串。

我知道这个问题是关于斜杠的,但我在搜索修剪斜杠时发现了这篇文章(在字符串文字的尾部和头部),因为人们需要这个解决方案,所以我在这里发布一个:

'///I am free///'.replace(/^\/+|\/+$/g, ''); // returns 'I am free'

更新

正如评论中提到的@Stephen R 一样,如果您想在字符串文字的尾部和头部同时删除斜杠和反斜杠,您可以这样写:

'\/\\/\/I am free\\///\\\\'.replace(/^[\\/]+|[\\/]+$/g, '') // returns 'I am free'

这个片段更准确:

str.replace(/^(.+?)\/*?$/, "$1");
  1. 它不是剥离/字符串,因为它是有效的 url。
  2. 它用多个尾随斜杠去除字符串。

基于@vdegenne 的回答......如何剥离:

单斜杠:

theString.replace(/\/$/, '');

单个或连续的尾部斜杠:

theString.replace(/\/+$/g, '');

单前导斜杠:

theString.replace(/^\//, '');

单个或连续的前导斜杠:

theString.replace(/^\/+/g, '');

单个前导和尾随斜杠:

theString.replace(/^\/|\/$/g, '')

单个或连续的前导和尾随斜杠:

theString.replace(/^\/+|\/+$/g, '')

要同时处理斜杠和反斜杠请将\/的实例替换为[\\/]

这里有一个小的 url 示例。

var currentUrl = location.href;

if(currentUrl.substr(-1) == '/') {
    currentUrl = currentUrl.substr(0, currentUrl.length - 1);
}

记录新的 url

console.log(currentUrl);

我知道的最简单的方法是:

function stripTrailingSlash(str){
   if(str.charAt(str.length-1) == "/"){ str = str.substr(0, str.length - 1);}
   return str
}

更新 ES2015 版本。

const stripTrailingSlash = str=>str.charAt(str.length-1)=="/"?str.substr(0,str.length-1):str;

然后这将检查最后是否有 / ,如果它在那里,请将其删除。 如果不是,它将按原样返回您的字符串。

只有一件事我还不能评论@ThiefMaster哇,你不关心memory,你只是为了一个if而运行一个substr吗?

修复了字符串上从零开始的索引的计算。

function stripTrailingSlash(text) {
    return text
        .split('/')
        .filter(Boolean)
        .join('/');
}

另一种解决方案。

其中一些示例比您可能需要的更复杂。 要从任何地方(前导或尾随)删除单个斜杠,您可以使用以下简单的方法:

let no_trailing_slash_url = site.replace('/', '');

完整示例:

let site1 = "www.somesite.com";  
let site2 = "www.somesite.com/";  

function someFunction(site)
{
    let no_trailing_slash_url = site.replace('/', '');
    return no_trailing_slash_url;
}

console.log(someFunction(site2)); // www.somesite.com

请注意, .replace(...)返回一个字符串,它不会修改调用它的字符串。

function someFunction(site) {
  if (site.indexOf('/') > 0)
    return site.substring(0, site.indexOf('/'));
  return site;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM