JavaScript 获取字符串的实际长度(不带实体)

发布于 2024-10-14 05:44:26 字数 84 浏览 2 评论 0原文

我需要确定可能包含 html 实体的字符串的长度。

例如“↓” (↓) 将返回长度 6,这是正确的,但我希望这些实体仅被计为 1 个字符。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凡间太子 2024-10-21 05:44:26
<div id="foo">↓</div>

alert(document.getElementById("foo").innerHTML.length); // alerts 1

因此,基于这个基本原理,创建一个 div,将混合的实体字符串附加到其中,提取 HTML 并检查长度。

var div = document.createElement("div");
div.innerHTML = "↓↓↓↓";
alert(div.innerHTML.length); // alerts 4

在这里尝试一下。

为了方便起见,您可能希望将其放入函数中,例如:

function realLength(str) { // maybe there's a better name?
    var el = document.createElement("div");
    el.innerHTML = str;
    return el.innerHTML.length;   
}
<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

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;   
}
毁我热情 2024-10-21 05:44:26

由于还没有使用 jQuery 的解决方案:

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

使用与 karim79 相同的方法,但它从不将创建的元素添加到文档中。

Since there's no solution using jQuery yet:

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

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

梦毁影碎の 2024-10-21 05:44:26

对于大多数用途,您可以假设“&”号后跟字母,或者可能是“#”和数字,后跟分号,都是一个字符。

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

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;
李不 2024-10-21 05:44:26

如果您在浏览器中运行 javascript,我建议您使用它来帮助您。您可以创建一个元素并将其innerHTML 设置为包含HTML 实体的字符串。然后将您刚刚创建的元素的内容提取为文本。

下面是一个示例(使用 Mootools): http://jsfiddle.net/mqchen/H73EV/

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/

我还不会笑 2024-10-21 05:44:26

不幸的是,JavaScript 本身并不支持 HTML 实体的编码或解码,这是您需要执行的操作才能获取“真实”字符串长度。我找到了这个能够解码和编码 HTML 实体的第三方库,它看起来工作得足够好,但不能保证它的完整性。

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

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

一场信仰旅途 2024-10-21 05:44:26

使用 ES6(引入了 codePointAt()):

function strlen (str) {
    let sl = str.length
    let chars = sl
    for (i = 0; i < sl; i++) if (str.codePointAt(i) > 65535) {
       chars--;
       i++;
    }
    return chars
}

请注意 charCodeAt() 的工作方式不同。

Using ES6 (introduces codePointAt():

function strlen (str) {
    let sl = str.length
    let chars = sl
    for (i = 0; i < sl; i++) if (str.codePointAt(i) > 65535) {
       chars--;
       i++;
    }
    return chars
}

Beware charCodeAt() does not work the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文