Javascript 返回不带斜杠的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6680825/
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
Return string without trailing slash
提问by Ryan
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?
我该怎么做呢?
回答by Chandu
Try this:
尝试这个:
function someFunction(site)
{
return site.replace(/\/$/, "");
}
回答by ThiefMaster
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. Use str.length - 1
instead if you need to support those ancient browsers.
注意:IE8 及更早版本不支持负 substr 偏移量。str.length - 1
如果您需要支持那些古老的浏览器,请改用。
回答by Seth Holladay
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 来询问字符串是否以某物结尾,这使得编写一个更清晰、更易读的函数成为可能。
const stripTrailingSlash = (str) => {
return str.endsWith('/') ?
str.slice(0, -1) :
str;
};
回答by ChronosLLC
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
是一个字符串。
回答by 1ven
This snippet is more accurate:
这个片段更准确:
str.replace(/^(.+?)\/*?$/, "");
- It not strips
/
strings, as it's a valid url. - It strips strings with multiple trailing slashes.
- 它不会去除
/
字符串,因为它是一个有效的 url。 - 它用多个尾部斜杠去除字符串。
回答by vdegenne
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 Rmentioned 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'
回答by Barkermn01
The easies way i know of is this
我知道的最简单的方法是这个
function stipTrailingSlash(str){
if(srt.charAt(str.length-1) == "/"){ str = str.substr(0, str.length - 1);}
return str
}
This will then check for a / on the end and if its there remove it if its not will return your string as it was
然后这将检查最后是否有一个 / ,如果它在那里删除它,如果它没有将返回你的字符串,因为它是
Just one thing that i cant comment on yet @ThiefMaster wow you dont care about memory do you lol runnign a substr just for an if?
只有一件事我还不能评论@ThiefMaster 哇,你不在乎内存,你是否只是为了一个 if 运行 substr ?
Fixed the calucation for zero-based index on the string.
修复了字符串上从零开始的索引的计算。
回答by DevJ3rry
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
记录新网址
console.log(currentUrl);
回答by Доширак
function stripTrailingSlash(text) {
return text
.split('/')
.filter(Boolean)
.join('/');
}
another solution.
另一种解决方案。
回答by Stephen R
Based on @vdegenne 's answer... how to strip:
基于@vdegenne 的回答......如何剥离:
Single trailing slash:
单尾斜杠:
theString.replace(/\/$/, '');
theString.replace(/\/$/, '');
Single or consecutive trailing slashes:
单个或连续的尾部斜杠:
theString.replace(/\/+$/g, '');
theString.replace(/\/+$/g, '');
Single leading slash:
单前导斜线:
theString.replace(/^\//, '');
theString.replace(/^\//, '');
Single or consecutive leading slashes:
单个或连续的前导斜杠:
theString.replace(/^\/+/g, '');
theString.replace(/^\/+/g, '');
Single leading and trailing slashes:
单个前导和尾随斜杠:
theString.replace(/^\/|\/$/g, '')
theString.replace(/^\/|\/$/g, '')
Single or consecutive leading and trailing slashes:
单个或连续的前导和尾随斜线:
theString.replace(/^\/+|\/+$/g, '')
theString.replace(/^\/+|\/+$/g, '')
To handle both slashes and backslashes, replace instances of \/
with [\\/]
为了处理这两个斜线和回斜线,更换的情况下,\/
与[\\/]