I have to replace the < and > in a string and with blank. Below is the piece of code :
var html = '<script src="http://example.com/stopsscript.js"></script>';
var charEscape = function(_html) {
var newHTML = _html;
console.log(newHTML+" 1");
newHTML = _html.replace(/[<>]/g, '');
return newHTML;
};
console.log(charEscape(html));
When i run this, i get Uncaught SyntaxError: Invalid or unexpected token in the 1st line ie
var html = '<script src="http://example.com/stopsscript.js"></script>';
Can someone tell me what i am doing wrong? Thanks in advance :)
You need to escape forward slash '/' character at the enclosing of the script tag by adding a backslash.
var html = '<script src="http://example.com/stopsscript.js"><\/script>';
console.log(html)
The reason why we need to do it is explained here.