How to remove/replace empty lines from string?

Originally posted by DanNad on November 09, 2022

Hello all

i cant get the empty lines to be removed.

Example:
i have a String like this:
Example:
Object is: page.querySelectorAll(“*”)[0].innerText
the innerText i get is similar to this:

Test Text

Test Text

Test Text

Test Text

Test Text Test Text

i want the text to be this:

Test Text
Test Text
Test Text
Test Text
Test Text Test Text

i tried to find a way with aqString methods but i just cant find a good way to remove the empty line.

what i try to accomplish is to get all the text from a page and save it to a simple text file

Is there a way to do this?
Help would be very much appreciated.

Originally posted by KB1 on November 09, 2022

This is what i’ve used:
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html

The trim method.

Originally posted by DanNad on November 09, 2022


KB1 wrote:

This is what i’ve used:
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html

The trim method.


I tried the trim method. But it’ll remove only leading/trailing spaces

What i found is replacing blank lines via regex. e.g.

innerText.replace(/^\s*$/gim, "")

But i am still looking for a solution to get all the text line by line and without empty lines for the whole page object

Originally posted by rraghvani on November 09, 2022

Have a look at https://www.freecodecamp.org/news/javascript-split-how-to-split-a-string-into-an-array-in-js/

You need to split your string into lines (array) and then remove empty lines (into a new array). This will give the output you require

Example,