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. Use str.length - 1
instead if you need to support those ancient browsers.
ES6 / ES2015 provides an API for asking whether a string ends with something, which enables writing a cleaner and more readable 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.
This snippet is more accurate:
str.replace(/^(.+?)\/*?$/, "$1");
- It not strips
/
strings, as it's a valid url. - It strips strings with multiple trailing slashes.
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 :
'\/\\/\/I am free\\///\\\\'.replace(/^[\\/]+|[\\/]+$/g, '') // returns 'I am free'
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?
Fixed the calucation for zero-based index on the string.
Here a small url example.
var currentUrl = location.href;
if(currentUrl.substr(-1) == '/') {
currentUrl = currentUrl.substr(0, currentUrl.length - 1);
}
log the new url
console.log(currentUrl);
function stripTrailingSlash(text) {
return text
.split('/')
.filter(Boolean)
.join('/');
}
another solution.
Based on @vdegenne 's answer... how to strip:
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 backslashes, replace instances of \/
with [\\/]
function someFunction(site) {
if (site.indexOf('/') > 0)
return site.substring(0, site.indexOf('/'));
return site;
}
来源:https://stackoverflow.com/questions/6680825/return-string-without-trailing-slash