简体   繁体   中英

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

and after marking it bold or Italic the count should remain same as 13.

The code I am using to count is jQuery(selector).text().length;

But its returning me the data, along with html tags.

and if I have written "My Count is 4" in Bold in editor. The count is increasing, because its counting the html <b></b> tags also.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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