JavaScript 获取字符串的实际长度(不带实体)
我需要确定可能包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因此,基于这个基本原理,创建一个 div,将混合的实体字符串附加到其中,提取 HTML 并检查长度。
在这里尝试一下。
为了方便起见,您可能希望将其放入函数中,例如:
So based on that rationale, create a div, append your mixed up entity ridden string to it, extract the HTML and check the length.
Try it here.
You might want to put that in a function for convenience, e.g.:
由于还没有使用 jQuery 的解决方案:
使用与 karim79 相同的方法,但它从不将创建的元素添加到文档中。
Since there's no solution using jQuery yet:
Uses the same approach like karim79, but it never adds the created element to the document.
对于大多数用途,您可以假设“&”号后跟字母,或者可能是“#”和数字,后跟分号,都是一个字符。
You could for most purposes assume that an ampersand followed by letters, or a possible '#' and numbers, followed by a semicolon, is one character.
如果您在浏览器中运行 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/
不幸的是,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
使用 ES6(引入了
codePointAt()
):请注意
charCodeAt()
的工作方式不同。Using ES6 (introduces
codePointAt()
:Beware
charCodeAt()
does not work the same way.