TinyMCE- Get plain text

前端 未结 3 867
北荒
北荒 2021-01-02 03:10

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

相关标签:
3条回答
  • 2021-01-02 03:51
    var rawtext = tinyMCE.activeEditor.getBody().textContent;
    
    0 讨论(0)
  • 2021-01-02 03:51

    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.

    0 讨论(0)
  • 2021-01-02 04:16

    Try this:

    var myText = tinyMCE.activeEditor.selection.getContent({ format: 'text' });
    
    0 讨论(0)
提交回复
热议问题