Fred J.
Fred J.

Reputation: 6039

Remove unwanted characters from string with regex

This Meteor server code using cheerio to extract text from html:

` const myLines = ResObj.$('.panel-body').text();

Which console.log the following:enter image description here

Using regex, how can I get only non-empty lines, like:

He is
not happy today
and needs to eay
select

I have tried few things for no avail, like replace(/(\W\r\n)/, "") and so on...

Upvotes: 1

Views: 200

Answers (1)

trincot
trincot

Reputation: 351128

You could use this:

myLines = '\r\n          \r\n      He is\r\n \r\n \r\n not happy today\r\n   \r\n   ';
myLines = myLines.replace(/\s*?(^|\n)\s*/g, '$1'); 
console.log(myLines);

Upvotes: 1

Related Questions