text() preserve line breaks
See original GitHub issueI 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(/&lt;br&gt;/gi,"n").replace(/(&lt;([^&gt;]+)&gt;)/gi, "");
}
}
return this;
} else {
if (document.body.innerText) {
return this[0].innerText;
} else {
return this[0].innerHTML.replace(/&lt;br&gt;/gi,"n").replace(/(&lt;([^&gt;]+)&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:
- Created 7 years ago
- Reactions:19
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:Turns
<br>
into linebreaks, converts everything else into plain text.edit: copypasting here for visibility, according to @issuefiler this one-liner works too:
This unfortunately won’t be implemented, as it diverges from browsers.