I need to determine the length of string which may contain html-entities.
For example "&darr ;" (↓) would return length 6, which is correct, but I want these entities to be counted as only 1 character.
<div id="foo">↓</div>
alert(document.getElementById("foo").innerHTML.length); // alerts 1
So based on that rationale, create a div, append your mixed up entity ridden string to it, extract the HTML and check the length.
var div = document.createElement("div");
div.innerHTML = "↓↓↓↓";
alert(div.innerHTML.length); // alerts 4
You might want to put that in a function for convenience, e.g.:
function realLength(str) { // maybe there's a better name?
var el = document.createElement("div");
el.innerHTML = str;
return el.innerHTML.length;
}