Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove line breaks from string

vineetharohra
Kilo Contributor

I have a string such as the following:

Tour CB21, 16 Place de I`Iris
Via San Simpliciano 1
abc
xyz

 

    pqr

 

I want to remove all line breaks from this string so that it looks like this:

Tour CB21, 16 Place de I`Iris Via San Simpliciano 1 abc xyz pqr

 

I tried following code:

myString = myString.replace(/\n\s*\n/g, " "); 

 

The max it does is:

 

Tour CB21, 16 Place de I`Iris
Via San Simpliciano 1
abc
xyz
 pqr

How do i bring the string in a single line ?
1 ACCEPTED SOLUTION

try this,

myString = myString.replace(/(\r\n|\n|\r)/gm,"").replace(/ {1,}/g," ");

View solution in original post

4 REPLIES 4

vineetharohra
Kilo Contributor

I tried this also:

myString  = myString .replace(/\n/g, ""); 

but it doesn't bring the string together in a single line.

Can you check what the below code is giving. What is the source of your string?

 

myString = myString.toString();

gs.print(myString);

try this,

myString = myString.replace(/(\r\n|\n|\r)/gm,"").replace(/ {1,}/g," ");

Hi Shishir,

 

Thank you for your reply.

Following line worked:

 

myString = myString.replace(/(\r\n|\n|\r)/gm,"");

 

Kind Regards,

Vineetha