0

まず第一に、出力:

var postContent = wysihtml5Textarea.data("wysihtml5").editor.getValue();

次のようになります。

<p>sf sdf asd asd  <b>asd</b> d asd ad&nbsp; asd&nbsp;</p> 

私は次のことを達成したい:

  1. すべてのHTMLタグを削除します
  2. すべて削除する&nbsp;
  3. 単語を数えます(たとえば、「ロックンロール」のような単語は1つの単語として数える必要があります)。

私はSOをブラウジングしてきましたが、次のことがわかりました。

 $(txt).text(); remove all HTML tags
 txt.replace(/&nbsp;/g, ''); // remove all &nbsp;
 txt.replace( /[^\w ]/g, "" ).split( /\s+/ ).length; // word count (not sure if it deals with hyphenated words)
 $('#word-count').html(wordCount); // and display it

これらすべてをきれいに混ぜる方法がわかりません。

助言がありますか?

4

1 に答える 1

1

このhttp://jsfiddle.net/adiioo7/QLgjs/1/を使用してみてください

 var str="<p>sf sdf asd asd  <b>asd</b> d asd ad&nbsp; asd&nbsp;</p>";

 alert("Plain Text : " + $(str).text());
 alert("Word Count : " +$(str).text().split(" ").length);
于 2013-02-19T07:43:29.490 に答える