2013-05-23 16 views
14

Desidero rimuovere tutti i tag html tranne <br> o <br/> da una stringa utilizzando javascript. Ho visto molte domande come questa, ma le loro risposte rimuoveranno tutti i tag html compresi i tag <br> e <br/>.Rimuovere tag HTML tranne <br> o <br/> tag con javascript

Qualcuno sa una regex per fare questo?

+0

se non ci sono
da nessuna parte, è sufficiente rimuovere altri tag html. – cp100

+0

Anche se aperto, questo è un duplicato di [questo] (http://stackoverflow.com/a/828647/263858) e [questo] (http://stackoverflow.com/a/18128321/263858) –

+0

Possibile duplicato di [Estrai codice HTML da testo in JavaScript eccetto tag p?] (Https://stackoverflow.com/questions/828572/strip-html-from-text-in-javascript-except-p-tags) – Wolfie

risposta

8

Try This

function remove_tags(html) 
{ 
    var html = html.replace("<br>","||br||"); 
    var tmp = document.createElement("DIV"); 
    tmp.innerHTML = html; 
    html = tmp.textContent||tmp.innerText; 
    return html.replace("||br||","<br>"); 
} 
+1

Questa non è la soluzione migliore in quanto se l'html conteneva già "|| br ||" prima della sostituzione, diventerebbero tutti "
". La soluzione di h2ooooooo è superiore poiché non ha questo problema. – Aura

34

Utilizzare un lookahead negativo (utilizzando un'espressione regolare come ad esempio /<(?!br\s*\/?)[^>]+>/g):

var html = 'this is my <b>string</b> and it\'s pretty cool<br />isn\'t it?<br>Yep, it is. <strong>More HTML tags</strong>'; 
html = html.replace(/<(?!br\s*\/?)[^>]+>/g, ''); 

console.log(html); 
//this is my string and it's pretty cool<br />isn't it?<br>Yep, it is. More HTML tags 

Demo

+0

Soluzione molto accurata . Molte grazie. Funziona come un fascino. – Logus

+0

Questo non rimuoverebbe il tag '', c'è una semplice modifica a questa regola regexp che specifica che 'br' deve essere da solo per rimanere? (ovviamente non è un tag HTML valido, ma cerca di essere completo) – Wolfie

+0

@Wolfie Vuoi solo corrispondere a '' e NON '
'? [Questa risposta (in php ma la regex dovrebbe essere abbastanza identica) accetta tutti i tag che iniziano con br] (https://stackoverflow.com/a/20196175/247893). – h2ooooooo

5

ho lavorato sulla ultimo suggerimento per sviluppare un funzione rimuovendo tutto o semplicemente mantenendo alcuni tag

function strip_tags(_html /*you can put each single tag per argument*/) 
{ 
    var _tags = [], _tag = "" ; 
    for(var _a = 1 ; _a < arguments.length ; _a++) 
    { 
     _tag = arguments[_a].replace(/<|>/g, '').trim() ; 
     if (arguments[_a].length > 0) _tags.push(_tag, "/"+_tag); 
    } 

    if (!(typeof _html == "string") && !(_html instanceof String)) return "" ; 
    else if (_tags.length == 0) return _html.replace(/<(\s*\/?)[^>]+>/g, "") ; 
    else 
    { 
     var _re = new RegExp("<(?!("+_tags.join("|")+")\s*\/?)[^>]+>", "g"); 
     return _html.replace(_re, ''); 
    } 
} 

var _html = "<b>Just</b> some <i>tags</i> and text to test <u>this code</u>" ; 
document.write("This is the original html code including some tags<br>"); 
document.write(_html + "<br><br>"); // original html code 
document.write("Now we remove all tags (plain text)<br>"); 
document.write(strip_tags(_html) + "<br><br>"); // remove all tags 
document.write("Only the bold tag is kept<br>"); 
document.write(strip_tags(_html, "b") + "<br><br>"); // keep <b> only 
document.write("Only the underline tag is kept<br>"); 
document.write(strip_tags(_html, "u") + "<br><br>"); // keep <u> only 
document.write("Only the italic tag is kept<br>"); 
document.write(strip_tags(_html, "<i>") + "<br><br>"); // keep <i> only 
document.write("Keeping both italic and underline<br>"); 
document.write(strip_tags(_html, "i", "u")); // keep both <i> and <u> 
1

Per espandere h2ooooooo risposta per includere gli spazi iniziali e di essere caso insenctive si potrebbe usare

/<(?!\s*br\s*\/?)[^>]+>/gi 
Problemi correlati