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.

How to split using empty new line as separator

kumaran
Giga Expert

Hi,

I working on script to parse a powershell output.

I need to splint the value using empty new line as a separator.

I tried different options but nothing is working.

Please help

1 ACCEPTED SOLUTION

kumaran
Giga Expert

Thanks for your response.


I made this to work, I splitted using the first string after the new line.


It worked now..


Thanks again


View solution in original post

4 REPLIES 4

Dennis R
Tera Guru

Can you do it using a regular expression?



var s = "This is a test\nLine 1\n\nLine 2\n\n\nLine3";


var sArray = s.split(/\n{2,}/);


// sArray = ["This is a test\nLine 1", "Line 2", "Line 3"];


Also, just to clarify, if you're using Powershell, you might be getting back \r\n instead of just \n.   If so, just change your regular expression from:


/\n{2,}/



to:


/(\r\n){2,}/



To be honest, I haven't played with Powershell that much, so I'm not sure whether it uses old DOS-style line endings or UNIX-style line endings.   Since it's Windows, I suspect the former.   If one doesn't work, try the other.  


matthew_magee1
Giga Guru

In line w/ Dennis' response:



....<script> + \n + <script>...



The '\n\ should give you a line break


kumaran
Giga Expert

Thanks for your response.


I made this to work, I splitted using the first string after the new line.


It worked now..


Thanks again