Search code examples
javascriptstringline

How to remove line that not contains any string? Angular


I have a string message but I have to remove the line which not containing any string. Because it will add more space to message container.

console.log(str);

The string ouput:

hello
how are you?

what's going on?


bye...


....more empty line below

I want it to be like:

hello
how are you?
what's going on?
bye...

Solution

  • You could do that by using replace function with a regexp. Example:

    var str = "hello \n how are you? \n\n what's going on?"
    console.log(str)
    console.log(str.replace(/(\n)+/gi, "\n"))