TinyMCE- Get plain text

久未见 提交于 2020-01-10 18:44:13

问题


In tinyMCE, Is there any way to get the plain text instead of HTML text?


回答1:


Try this:

var myText = tinyMCE.activeEditor.selection.getContent({ format: 'text' });



回答2:


var rawtext = tinyMCE.activeEditor.getBody().textContent;



回答3:


I just tried this approach:

editor.getContent()
   .replace(/<[^>]*>/ig, ' ')
   .replace(/<\/[^>]*>/ig, ' ')
   .replace(/&nbsp;|&#160;/gi, ' ')
   .replace(/\s+/ig, ' ')
   .trim();
  • Replaces both opening and closing html tags with space
  • Replaces various known special characters with space (add yours as well)
  • Replaces multiple spaces with a single space

Worked reasonably well, but it is obviously not perfect. I need only an approximation of plain text for purposes of word counting, so I am willing to ignore corner cases such as having part of the word bold or italic (replacement above for <b>a</b><i>x</i> will produce two separate words a b instead of ab).

It is an extension of Regular expression to remove HTML tags from a string

Hope that helps.



来源:https://stackoverflow.com/questions/5288083/tinymce-get-plain-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!