Visualizzazione post con etichetta br2nl. Mostra tutti i post
Visualizzazione post con etichetta br2nl. Mostra tutti i post

martedì 19 gennaio 2010

Javascript - nl2br e br2nl

Oggi ho sbattuto un po' la testa su javascript per un problema stupido, ma dato che le cose stupide sono quelle riutilizzate più spesso... Comunque, non escludo la presenza di funzioni "ufficiali", ma visto che è soddisfacente sbattere la testa ho scritto queste due funzioncine

String.prototype.nl2br = function() {
var breakTag = '<br />';
return (this + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

String.prototype.br2nl = function() {
var breakTag = '\n';
return (this + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

la parte interessante è il poterle poi richiamare in questo modo

myString.nl2br(); myString.br2nl();