question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

text() preserve line breaks

See original GitHub issue

I need to access innerHTML and preserve line breaks, so where is like <br>. In jQuery a solution could be this one

(function($){
   $.fn.innerText = function(msg) {
         if (msg) {
            if (document.body.innerText) {
               for (var i in this) {
                  this[i].innerText = msg;
               }
            } else {
               for (var i in this) {
                  this[i].innerHTML.replace(/&amp;lt;br&amp;gt;/gi,"n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");
               }
            }
            return this;
         } else {
            if (document.body.innerText) {
               return this[0].innerText;
            } else {
               return this[0].innerHTML.replace(/&amp;lt;br&amp;gt;/gi,"n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");
            }
         }
   };
})(jQuery);

so that you can easily use innerText() function like

 $(this).find('a').each(function(index,item) {
      console.log( $(this).innerText() )
   })

But how to do this in cheerio?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

39reactions
msikmacommented, Jun 1, 2022

I’m not sure if this workaround is good enough for your purposes, but I needed to quickly fetch some text from an HTML document with <br> converted to linebreaks. I ended up doing this:

$('.cc-newsbody').find('br').replaceWith('\n')
const text = $('.cc-newsbody').text() // can't chain

Turns <br> into linebreaks, converts everything else into plain text.

edit: copypasting here for visibility, according to @issuefiler this one-liner works too:

$(".test").find("br").replaceWith("\n").end().text()
10reactions
fb55commented, Dec 22, 2020

This unfortunately won’t be implemented, as it diverges from browsers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preserve line breaks in textarea - html - Stack Overflow
In that case you have to preserve the original line breaks just as received. So what you typically do is: you save the...
Read more >
How to Preserve Newlines, Line Breaks, and Whitespace in ...
If you want your text to overflow the parent's boundaries, you should use pre as your CSS whitespace property. Using white-space: pre wraps ......
Read more >
How to Render an HTML String Preserving Spaces and Line ...
In this tutorial, you can learn how to render an HTML text preserving spaces and line breaks. You need to use the HTML...
Read more >
Preserving Line Breaks When Using Jsoup | Baeldung
In this tutorial, we'll look briefly at the different ways of preserving line breaks when using Jsoup to parse HTML to plain text....
Read more >
How to preserve line breaks using a CDS with Dynamics
In the Power Automate flow, we're going to use the REPLACE() function to replace all of the carriage returns in the string with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found