I have a script which allows to replace undesired HTML tags and escape quotes to "improve" security and prevent mainly script tag and onload injection, etc.... This script is used to "texturize" content retrieved from innerHTML
.
However, it multiples near by 3 my execution time (in a loop). I would like to know if there is a better way or better regex to do it:
function safe_content( text ) {
text = text.replace( /<script[^>]*>.*?<\/script>/gi, '' );
text = text.replace( /(<p[^>]*>|<\/p>)/g, '' );
text = text.replace( /'/g, '’' ).replace( /'/g, '’' ).replace( /[\u2019]/g, '’' );
text = text.replace( /"/g, '”' ).replace( /"/g, '”' ).replace( /"/g, '”' ).replace( /[\u201D]/g, '”' );
text = text.replace( /([\w]+)=&#[\d]+;(.+?)&#[\d]+;/g, '$1="$2"' );
return text.trim();
};
EDIT: here a fiddle: https://fiddle.jshell.net/srnoe3s4/1/. Fiddle don't like script
tags in javascript string apparently so I didn't add it.