Tuesday 15 September 2015

Regex to remove and its content in Javascript -


I am trying to remove the script and their content from the HTML body and it has come so far when

  just_text = just_text.replace (/ & lt; \ s * script [^> *] ( * *] | $) / Ig, '');   

It does not want to work, I still get the content.

Can you help me?

Thanks

Answers like these questions are always the same: Regular expressions are used Do not Instead, parse HTML, modify the DOM and resear it over HTML if you need it.

Example:

  var container = document.createElement ('div'); Container. Piler html = just_text; // Find and delete script 'elements' var scripts = container.get elementsbignnam ('script'); For (var i = scripts.length; i--;) {script [i] Parent.Node.removeChild (script [i]); } Just_text = container.innerHTML;   

If you want to delete the script tag from the page, then it is basically the same:

  var scripts = Document .body.getElementsByTagName (such as script '); For (var i = scripts.length; i--;) {script [i] Parent.Node.removeChild (script [i]); }    

No comments:

Post a Comment