JavaScript Get real length of a string (without entities)

微笑、不失礼 提交于 2019-12-31 19:28:03

问题


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.


回答1:


<div id="foo">&darr;</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 = "&darr;&darr;&darr;&darr;";
alert(div.innerHTML.length); // alerts 4

Try it here.

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;   
}



回答2:


Since there's no solution using jQuery yet:

var str = 'lol&amp;';
alert($('<span />').html(str).text().length); // alerts 4

Uses the same approach like karim79, but it never adds the created element to the document.




回答3:


You could for most purposes assume that an ampersand followed by letters, or a possible '#' and numbers, followed by a semicolon, is one character.

var strlen=string.replace(/&#?[a-zA-Z0-9]+;/g,' ').length;



回答4:


If you are running the javascript in a browser I would suggest using it to help you. You can create an element and set its innerHTML to be your string containing HTML-entities. Then extract the contents of that element you just created as text.

Here is an example (uses Mootools): http://jsfiddle.net/mqchen/H73EV/




回答5:


Unfortunately, JavaScript does not natively support encoding or decoding of HTML entities, which is what you will need to do to get the 'real' string length. I was able to find this third-party library which is able to decode and encode HTML entities and it appears to work well enough, but there's no guaranteeing how complete it will be.

http://www.strictly-software.com/htmlencode



来源:https://stackoverflow.com/questions/4788242/javascript-get-real-length-of-a-string-without-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!