[英]How to count the number of words in wysiwyg editor javascript?
我正在使用所见即所得的编辑器,在其中编写并保存了它。
我只想计算我写的字符数。
假设我写了"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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.