Return string without trailing slash

Ryan picture Ryan · Jul 13, 2011 · Viewed 132.8k times · Source

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?

Answer

Chandu picture Chandu · Jul 13, 2011

Try this:

function someFunction(site)     
{     
    return site.replace(/\/$/, "");
}