簡體   English   中英

如何在所見即所得編輯器javascript中計算單詞數?

[英]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('&nbsp;','');

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

看看這個小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM