我正在使用所见即所得编辑器,在那里编写并保存它。
我只是想计算我写的字符数。
假设我写了"My Count is 4"
,那么它应该显示计数为 13
将其标记为粗体或斜体后,计数应保持为 13。
我用来计数的代码是 jQuery(selector).text().length;
但它返回了数据以及 html 标签。
如果我写了 "My Count is 4"
在编辑器中以粗体显示。计数正在增加,因为它计数了 html <b></b>
标签也。
请帮我找到解决方案。
最佳答案
尝试这样的事情:
// firstly we'll strip the html out
var myCode = jQuery('#getMe').html();
// strip out tags and line breaks
var cleanCode = myCode.replace(/<(?:.|\n)*?>/gm, '').replace(/(\r\n|\n|\r)/gm,"").replace(' ','');
// then count as normal
var numChars = cleanCode.trim().length;
关于javascript - 如何计算所见即所得编辑器javascript中的单词数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34586448/