Atreyee Roy
Atreyee Roy

Reputation: 45

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

var html = '<script src="http://example.com/stopsscript.js"></script>';

Can someone tell me what i am doing wrong? Thanks in advance :)

Upvotes: 1

Views: 2643

Answers (1)

Mμ.
Mμ.

Reputation: 8542

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.

Upvotes: 2

Related Questions