简体   繁体   English

Javascript替换给出了Uncaught SyntaxError:无效或意外的令牌

[英]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.

相关问题 错误……未捕获的语法错误:无效或意外的令牌……使用 JavaScript - error… Uncaught SyntaxError: Invalid or unexpected token… with JavaScript 未捕获的SyntaxError:具有Scala的javascript中的无效或意外令牌 - Uncaught SyntaxError: Invalid or unexpected token in javascript with scala javascript-未捕获的SyntaxError:无效或意外的令牌 - javascript - Uncaught SyntaxError: Invalid or unexpected token 未捕获的SyntaxError:无效或意外的令牌 - Uncaught SyntaxError: Invalid or unexpected token 在javascript中结合php(未捕获的语法错误:无效或意外的令牌) - Combining php in javascript (Uncaught SyntaxError: Invalid or unexpected token) 使用javascript将内容添加到div-“未捕获的SyntaxError:无效或意外的令牌”? - Adding content to div with javascript - 'Uncaught SyntaxError: Invalid or unexpected token'? Magento 2 cms 后端 javascript 错误 Uncaught SyntaxError: Invalid or unexpected token - Magento 2 cms backend javascript error Uncaught SyntaxError: Invalid or unexpected token Javascript Uncaught SyntaxError:意外的令牌 - Javascript Uncaught SyntaxError: Unexpected token < Javascript未捕获的SyntaxError:意外令牌{ - Javascript Uncaught SyntaxError: Unexpected token { JavaScript错误:未捕获的SyntaxError:第1行上的无效或意外令牌 - JavaScript error: Uncaught SyntaxError: Invalid or unexpected token on line 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM