[英]Return string without trailing slash
I have two variables:我有两个变量:
site1 = "www.somesite.com";
site2 = "www.somesite.com/";
I want to do something like this我想做这样的事情
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;
}
How do I do this?我该怎么做呢?
Try this:尝试这个:
function someFunction(site)
{
return site.replace(/\/$/, "");
}
function stripTrailingSlash(str) {
if(str.substr(-1) === '/') {
return str.substr(0, str.length - 1);
}
return str;
}
Note: IE8 and older do not support negative substr offsets.注意:IE8 和更早版本不支持负 substr 偏移。 Use
str.length - 1
instead if you need to support those ancient browsers.如果您需要支持那些古老的浏览器,请改用
str.length - 1
。
ES6 / ES2015 provides an API for asking whether a string ends with something, which enables writing a cleaner and more readable function. ES6 / ES2015 提供了一个 API 用于询问字符串是否以某些内容结尾,这使得编写更清晰、更易读的 function 成为可能。
const stripTrailingSlash = (str) => {
return str.endsWith('/') ?
str.slice(0, -1) :
str;
};
I'd use a regular expression:我会使用正则表达式:
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 ($)
}
You'll want to make sure that the variable site
is a string, though.不过,您需要确保变量
site
是一个字符串。
I know the question is about trailing slashes but I found this post during my search for trimming slashes (both at the tail and head of a string literal), as people would need this solution I am posting one here:我知道这个问题是关于斜杠的,但我在搜索修剪斜杠时发现了这篇文章(在字符串文字的尾部和头部),因为人们需要这个解决方案,所以我在这里发布一个:
'///I am free///'.replace(/^\/+|\/+$/g, ''); // returns 'I am free'
UPDATE :更新:
as @Stephen R mentioned in the comments, if you want to remove both slashes and backslashes both at the tail and the head of a string literal, you would write:正如评论中提到的@Stephen R 一样,如果您想在字符串文字的尾部和头部同时删除斜杠和反斜杠,您可以这样写:
'\/\\/\/I am free\\///\\\\'.replace(/^[\\/]+|[\\/]+$/g, '') // returns 'I am free'
This snippet is more accurate:这个片段更准确:
str.replace(/^(.+?)\/*?$/, "$1");
/
strings, as it's a valid url./
字符串,因为它是有效的 url。Based on @vdegenne 's answer... how to strip:基于@vdegenne 的回答......如何剥离:
Single trailing slash:单斜杠:
theString.replace(/\/$/, '');
Single or consecutive trailing slashes:单个或连续的尾部斜杠:
theString.replace(/\/+$/g, '');
Single leading slash:单前导斜杠:
theString.replace(/^\//, '');
Single or consecutive leading slashes:单个或连续的前导斜杠:
theString.replace(/^\/+/g, '');
Single leading and trailing slashes:单个前导和尾随斜杠:
theString.replace(/^\/|\/$/g, '')
Single or consecutive leading and trailing slashes:单个或连续的前导和尾随斜杠:
theString.replace(/^\/+|\/+$/g, '')
To handle both slashes and back slashes, replace instances of \/
with [\\/]
要同时处理斜杠和反斜杠,请将
\/
的实例替换为[\\/]
Here a small url example.这里有一个小的 url 示例。
var currentUrl = location.href;
if(currentUrl.substr(-1) == '/') {
currentUrl = currentUrl.substr(0, currentUrl.length - 1);
}
log the new url记录新的 url
console.log(currentUrl);
The easiest way I know of is this:我知道的最简单的方法是:
function stripTrailingSlash(str){
if(str.charAt(str.length-1) == "/"){ str = str.substr(0, str.length - 1);}
return str
}
Updates ES2015 version.更新 ES2015 版本。
const stripTrailingSlash = str=>str.charAt(str.length-1)=="/"?str.substr(0,str.length-1):str;
This will then check for a / on the end and if it's there, remove it.然后这将检查最后是否有 / ,如果它在那里,请将其删除。 If it's not, it will return your string as it was.
如果不是,它将按原样返回您的字符串。
Just one thing that I can't comment on yet @ThiefMaster wow you don't care about memory do you lol running a substr just for an if?只有一件事我还不能评论@ThiefMaster哇,你不关心memory,你只是为了一个if而运行一个substr吗?
Fixed the calculation for zero-based index on the string.修复了字符串上从零开始的索引的计算。
function stripTrailingSlash(text) {
return text
.split('/')
.filter(Boolean)
.join('/');
}
another solution.另一种解决方案。
Some of these examples are more complicated than you might need.其中一些示例比您可能需要的更复杂。 To remove a single slash, from anywhere (leading or trailing), you could get away with something as simple as this:
要从任何地方(前导或尾随)删除单个斜杠,您可以使用以下简单的方法:
let no_trailing_slash_url = site.replace('/', '');
Complete example:完整示例:
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
Note that .replace(...)
returns a string, it does not modify the string it is called on.请注意,
.replace(...)
返回一个字符串,它不会修改调用它的字符串。
function someFunction(site) {
if (site.indexOf('/') > 0)
return site.substring(0, site.indexOf('/'));
return site;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.