How to get plain text from summernote editor?

for example, this is what I entered:

sdf 42342 xxcv 

.code() converts it to sdf<br>42342<br>xxcv

or other:

 [{"_type":"ServerOperation","operationType":"ANNOUNCE"}] 

becames

 <span class="message_content">[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]</span> 

How to get clean / plain text?

+11
source share
6 answers

You can apply one of the two best answers to your JavaScript question : how to cut HTML tags from a string? after .code() to remove tags.

ReactiveRaven's answer (to keep it on one line) works fine for me:

 cleanText = $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, ""); 

For example, With [{"_type":"ServerOperation","operationType":"ANNOUNCE"}] :

  • $("#summernote").code() returns

    <p>[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]<br></p>

  • $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "") returns

    [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

    without any tags.


And if you want to keep the carriage return, you can replace </p> and <br> with \n before applying the solution indicated in the related question:

 $("#summernote").code() .replace(/<\/p>/gi, "\n") .replace(/<br\/?>/gi, "\n") .replace(/<\/?[^>]+(>|$)/g, ""); 
+14
source

Just try the following:

 var plainText = $($("#summernote").code()).text() 

EDIT : In newer versions, you need to use this instead:

 var plainText = $($("#summernote").summernote("code")).text() 
+20
source

you can use this

  var code = $("#editor").code(); var text = code.replace(/<p>/gi, " "); var plainText= $("<div />").html(text).text(); 
+3
source

newer versions

 var contents = $('#summernote').summernote('code'); var plainText = $("<p>" + contents+ "</p>").text(); 

Adding a dummy tag

eliminates the lack of formatting.

+2
source

I needed to check if the text area has any content. This did the trick:

Current version of Summernote (8):

 $($("#summernote").summernote('code').replace(/&nbsp;|<br>/g, ' ')).text().trim() == '' 

In my old version:

 $($("#summernote").code().replace(/&nbsp;|<br>/g, ' ')).text().trim() == '' 
+1
source

Try it :)

 $('#summernote').summernote ('code', '<b> hello world </ b>'); 
0
source

Source: https://habr.com/ru/post/984893/


All Articles