How do I clean the html tags of a string?

1

I found the problem of HTML tags inside a String, the string I take from a SharePoint RTE-field:

var clientContext;
var list;
var item;

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

   // Create an instance of the current context.
function sharePointReady() {
   clientContext = SP.ClientContext.get_current();
   list = clientContext.get_web().get_lists().getByTitle("Prova Scheda");
   var itemId = parseInt(GetUrlKeyValue('ID'))
   item = list.getItemById(itemId);
   clientContext.load(item);
   clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
   var corpo = item.get_item('Corpo');
   var corpoOr = item.get_item('CorpoOriginale');
   corpoOr = corpoOr.replace("<p>", "").replace("<div>", " ").replace("<br>", "\n").replace("<html>", " ").replace("</p>", "").replace("</div>", "").replace("</html>", "");
corpo = corpo.replace("<p>", "").replace("<div>", " ").replace("<br>", "\n").replace("<html>", " ").replace("</p>", "").replace("</div>", "").replace("</html>", "");
console.log(corpo + " " + corpoOr);
}
function onRequestFailed(sender, args) {
   console.log('Error: ' + args.get_message());
}´

taking the variable "corpo" returns a String with the html Tags to the internal one, provide with: corpo.textContent , corpo.innerText and corpo.outerHtml but in the 3 cases it returns an indefinite value.

in the code that I put, as you can see I am using the replace() but it also returns the html tags as you can see in the screenshot how to clean all the tags and leave only the testo?

    
asked by Federico Iannarelli 24.08.2018 в 12:19
source

1 answer

2

You could use a regular expression:

var texto = '<p>Elemento con etiquetas <strong>que quiero</strong> que desaparezcan</p>';

var sin_tags = texto.replace(/(<([^>]+)>)/ig,"");

alert(sin_tags);

In Jquery, there is the stripHTML () function that is dedicated to making you work. But this is the solution I see most simple in Javascript.

    
answered by 24.08.2018 / 12:42
source