Javascript 返回不带尾随斜杠的字符串
我有两个变量:Javascript 返回不带尾随斜杠的字符串,javascript,string,trailing-slash,Javascript,String,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_
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 stripTrailingSlash(str) {
if(str.substr(-1) === '/') {
return str.substr(0, str.length - 1);
}
return str;
}
注:IE8及以上版本不支持负substr偏移。如果您需要支持那些古老的浏览器,请使用str.length-1
。尝试以下方法:
function someFunction(site) {
if (site.indexOf('/') > 0)
return site.substring(0, site.indexOf('/'));
return site;
}
function someFunction(site)
{
return site.replace(/\/$/, "");
}
我会使用正则表达式:
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
是一个字符串。据我所知,最简单的方法是
function stipTrailingSlash(str){
if(srt.charAt(str.length-1) == "/"){ str = str.substr(0, str.length - 1);}
return str
}
然后,这将检查末尾是否有a/并且如果有,则删除它,如果没有,则返回原来的字符串
只有一件事我还不能评论
@ThiefMaster哇,你不在乎记忆,你只是为了一个if而运行一个substr吗
修复了基于字符串的基于零的索引的计算 这里是一个小url示例
var currentUrl = location.href;
if(currentUrl.substr(-1) == '/') {
currentUrl = currentUrl.substr(0, currentUrl.length - 1);
}
记录新的url
console.log(currentUrl);
ES6/ES2015提供了一个API,用于询问字符串是否以某物结尾,从而可以编写更干净、更可读的函数
const stripTrailingSlash = (str) => {
return str.endsWith('/') ?
str.slice(0, -1) :
str;
};
这个片段更准确:
str.replace(/^(.+?)\/*?$/, "$1");
/
字符串,因为它是一个有效的url我知道问题是关于尾随斜杠的,但我在搜索修剪斜杠(在字符串文字的尾部和头部)时发现了这篇文章,因为人们需要这个解决方案,我在这里发布了一个:
'///I am free///'.replace(/^\/+|\/+$/g, ''); // returns 'I am free'
更新: 正如@Stephen R在评论中提到的,如果您想删除字符串文字尾部和头部的斜杠和反斜杠,您可以编写:
'\/\\/\/I am free\\///\\\\'.replace(/^[\\/]+|[\\/]+$/g, '') // returns 'I am free'
另一个解决方案。基于@vdegenne的答案。。。如何脱衣: 单尾斜杠:
string.replace(/\/$/,“”)代码>
单个或连续的尾随斜杠:
theString.replace(/\/+$/g')代码>
单前导斜杠:
string.replace(/^\/,“”)代码>
单个或连续的前导斜杠:
theString.replace(/^\/+//g')代码>
单个前导斜杠和尾随斜杠:
theString.replace(/^\/\/$/g')
单个或连续的前后斜杠:
theString.replace(/^\/+\/+$/g')
要同时处理斜杠和反斜杠,请将\/
的实例替换为[\\/]
其中一些示例比您可能需要的更复杂。要从任意位置(前导或尾随)删除一条斜线,您可以使用以下简单方法:
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(…)
返回一个字符串,它不会修改调用它的字符串。子字符串
?除此之外,它还删除了第一条斜线及其后的所有内容。@ThiefMaster:真的吗?你不知道我的意思是子字符串
?同样是的,我的意思是删除第一个斜杠及其后的所有内容,因为它确实填补了发布的问题和示例数据的账单。他的评论说他想删除尾随的斜杠slash@ThiefMaster:根据他的例子,我的代码是这样的。只是一个问题:如果URL更改为“完全限定”,为什么不在任何时候使用它有HTTP:/ /这不会正常工作,任何与中间的链接都不能用于GooGelSert处理多个尾随斜线的情况,您可以使用:<代码>返回站点。代码>“/”。替换(/\/$/,“”)将为“”。“/”是一个有效路径,不应删除。我完全同意,任何时候编写正则表达式时,都应该将其包装在一个带有描述性名称或注释的函数中。这才是真正的答案!如果您想同时修剪斜杠和反斜杠:。替换(/^[\\/]+\\/]+$/g',)
2019年的最佳解决方案@PaulRad在使用此解决方案时,单元测试中未涉及str.slice(0,-1)分支的问题。如何解决这个问题?@BijayRai这意味着您的测试缺少一个案例,即str
以/
斜杠结尾。你应该有两个断言(或两个测试),一个带斜杠,一个不带斜杠。虽然这可能是删除多个斜杠的有效方法,但这不是OP.@JonL的要求。许多人浏览堆栈溢出以找到答案。我觉得这个答案比公认的答案更有用。我认为这更简单->(.+?)\/+$
。首先不需要使用^
,因为
无论如何都会从一开始就搜索,在$
之前不需要使用*?
因为当正则表达式模式不匹配时,它将返回原始字符串。非常喜欢这里使用术语“古代”。
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