SORU
13 Temmuz 2011, ÇARŞAMBA


Bölü çizgisi ile bitmeyen bir dönüş dize

İki değişken var:

site1 = "www.somesite.com";  
site2 = "www.somesite.com/";  

Böyle bir şey yapmak istiyorum

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;
}

Bunu nasıl yapabilirim?

CEVAP
13 Temmuz 2011, ÇARŞAMBA


function stripTrailingSlash(str) {
    if(str.substr(-1) === '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
}

Not: ve eski olumsuz substr uzaklıklar desteklemez IE8. Eğer bu eski tarayıcıları desteklemek gerekiyorsa yerine str.length - 1 kullanın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BlackBoxTV

    BlackBoxTV

    7 Mayıs 2007
  • Fubar Protocol

    Fubar Protoc

    21 AĞUSTOS 2010
  • listedabive

    listedabive

    30 Ocak 2007