【发布时间】:2023-03-04 18:20:01
【问题描述】:
我在JS中有如下条件语句:
if(url === 'http://www.productionlocations.com/locations' || url === 'http://www.productionlocations.com/locations/')
我正在努力提高效率,所以我尝试了这个:
function stripTrailingSlash(str) {
if(str.substr(-1) == '/') {
return str.substr(0, str.length - 1);
}
return str;
}
theurl = stripTrailingSlash(url);
if(theurl === 'http://www.productionlocations.com/locations')
但显然这只会使它变成更多代码:)
最有效的方法是什么?我之前尝试过使用 indexOf() ,但是没有用。感谢您的帮助!
【问题讨论】:
-
条件语句不剥离任何东西。如果剥离不是必需的,请不要这样做!
-
删除尾部斜线的更简单方法:
str.replace(/\\$/, '')
标签: javascript string url