简体   繁体   English

如何在所见即所得编辑器javascript中计算单词数?

[英]How to count the number of words in wysiwyg editor javascript?

I am using WYSIWYG editor, written there and saved it. 我正在使用所见即所得的编辑器,在其中编写并保存了它。

I just want to count the number of characters I have written. 我只想计算我写的字符数。

Suppose I have written "My Count is 4" , Then it should show me the count as 13 假设我写了"My Count is 4" ,那么它应该显示为13

and after marking it bold or Italic the count should remain same as 13. 并将其标记为粗体或斜体后,计数应保持为13。

The code I am using to count is jQuery(selector).text().length; 我用来计数的代码是jQuery(selector).text().length;

But its returning me the data, along with html tags. 但是它返回了我的数据以及html标签。

and if I have written "My Count is 4" in Bold in editor. 并且如果我在编辑器中以粗体字写了"My Count is 4" The count is increasing, because its counting the html <b></b> tags also. 计数在增加,因为它也计数html <b></b>标记。

Please help me to find the solution. 请帮助我找到解决方案。

Try something like this: 尝试这样的事情:

// 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('&nbsp;','');

// then count as normal
var numChars = cleanCode.trim().length;

Have a look at this fiddle 看看这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM