JavaScriptでのURL末尾スラッシュ除去
正規表現で
正規表現で置換するパターン。
function removeTrailingSlash(url) { return url.replace(/\/$/, '') }
String.prototype.substr()
で
substr
で判定するパターン。
function removeTrailingSlash(url) { if (url.substr(-1) === '/') { url.substr(0, url.length - 1) } return url }
String.prototype.endsWith()
で
判定はendsWith
でやるパターン。es2015+で利用できる。
function removeTrailingSlash(url) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url }