2009-02-10 5 views
1

Le code suivant fonctionne, mais je suis sûr qu'il ya une façon plus compacte pour obtenir le même résultat, en particulier le remplacement de la chaîne:Existe-t-il un meilleur moyen d'écrire ce javascript regex/remplacer?

$('#nav-main .nav-sub').each(function() { 

    var name = $(this).attr('id'); 

    $(this).children('li').children('a').each(function() { 

     var text = $(this).text().toLowerCase(); 
     var spaces =//g; 
     var sub = /sub-/; 
     var id = name + '-' + text.replace(spaces, '-');  
     var id = id.replace(sub, '');  
     $(this).attr('id', id); 

    }); 

}); 

Répondre

1
$('#nav-main .nav-sub').each(function() { 
    var name = $(this).attr('id'); 
    $('li a', this).each(function() {  
     this.id = (name + '-' + $(this).text().toLowerCase().replace(/\s/g, '-')).replace(/sub-/, ''); 
    }); 
});