-5

I am trying to write an algorithm where I replace characters like &, <, >, , ', " with their HTML entity counterpart(i.e. & would be replaced with &amp;. This requires me to put a semi-colon in a string, which creates an error because Javascript thinks that I have ended the line. Here is my code:

str.charAt(i) = '&amp;';

I thought that I could cancel it like so:

str.charAt(i) = '&amp\;';

but this creates an error.

2
  • 7
    Semi-colons inside string literals do not terminate statements in JavaScript. You are misinterpreting whatever problem you are having.
    – Quentin
    Nov 16, 2015 at 16:58
  • ; isn't an html metacharacter. it's meaning unless unless preceeded with an &, so it by itself doesn't need to be escaped. and ; inside a string doesn't terminate the string.
    – Marc B
    Nov 16, 2015 at 16:58

5 Answers 5

4

The problem is that your assignment is invalid. You seem to be trying to mutate a string, which is impossible in JS since strings are immutable.

Instead you need to create a new string with the parts you want replaced with the new content.

var new_str = s.replace(/(?:(&)|(<)|(>)|(')|("))/g, function(s, g1,g2,g3,g4,g5) {
    if (g1) return "&amp;"
    if (g2) return "&lt;"
    if (g3) return "&gt;"
    if (g4) return "&apos;"
    if (g5) return "&quot;"
});
3

Semi-colons inside string literals do not terminate statements in JavaScript.

ReferenceError: Invalid left-hand side in assignment

Your problem is that you are trying to assign a value to the return value of a function, which isn't allowed.

If you want to replace characters in a string, use the replace method.

In this case, you are trying to convert a string of text into a string of HTML and it is simpler to let the DOM do that for you:

var div_node = document.createElement('div');
var text_node = document.createTextNode(str);
div_node.appendChild(text_node);
var str_html = div_node.innerHTML;

Note that this won't replace all of the characters. Some don't need escaping all the time.

2

You don't need to escape semicolons inside strings. JavaScript doesn't think the line has ended in a string any more than it thinks the line ended inside for (var i = 0; ...).

2
  • Thank you, somehow, I was receiving an error for the semi-colon being an unrecognized character. Nov 17, 2015 at 20:31
  • Unless you do. There are many formats that use a semicolon to separate elements.
    – Jiří
    Feb 23, 2021 at 16:46
2

The problem isn't an escaping issue. It's that you're attempting to assign a value to the static output of the String.charAt() method, which is not allowed.

Instead, try using the string replace method to replace your HTML entities with alternative values.

s = 'Hello & World';
s.replace(/\&/g, '&amp;');
2
  • That's only going to replace the first one, and he is also trying to replace <, > and " and '.
    – Quentin
    Nov 16, 2015 at 17:02
  • 1
    @Quentin - nevertheless, it demonstrates a solution to the problem in the code he posted. Nov 16, 2015 at 17:03
2

You can use a function to replace characters by their HTML entities:

function htmlEntities(str) {
    return String(str)
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&apos;');
}

; don't need any replacement.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.