[英]Javascript replace giving Uncaught SyntaxError: Invalid or unexpected token
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 当我运行它时,我在第一行中得到Uncaught SyntaxError:无效或意外的令牌 ,即
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. 您需要通过添加反斜杠在script标记的开头转义正斜杠“ /”字符。
var html = '<script src="http://example.com/stopsscript.js"><\\/script>'; console.log(html)
The reason why we need to do it is explained here . 为什么我们需要做的原因解释这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.