Splitting Multiline

goochg79
Kilo Explorer

Hi,

We have a multiline field on a item which we need to split into the indvidual lines and process these in a JS Script.

Is it possible to convert the multiline into an array, we have tried lines.split ('\n'); and also using match but neither have worked.

1 ACCEPTED SOLUTION

poyntzj
Kilo Sage

I use this in a recent bit of code.   reads a multiline variable.



strUserList = srigr.variables.user_list.toString();


arrUsers=strUserList.match(/[^\r\n]+/g);


View solution in original post

8 REPLIES 8

sunilsafare
Mega Guru

Hi Gareth,



If it is of journal_type then split('\n\n') will work some times. Could you pls elaborate your requirement



Thanks,


Sunil Safare


goochg79
Kilo Explorer

Hi Sunil,



The multiline is a variable on a catalog item.


It has several pieces of information seperated on the lines.


As part of the workflow we need to take this information and act on what is contained in the lines.


So I was planning on taking the variable and spliting this into an array and dealing with each line.


eg;


var mylines = current,variables.myLines;


var lines = mylines.split('\n');


for (var i = 0; i <= lines.length; i++) {


  prcoess (lines[i]);


}



eg of myLines is made up of;


  • staff; 12
  • temps:4
  • totalPeople: 16


Does this make sense?


Hi Gareth,



Try to cast the multiline variable to a String like so, I'm not sure if it will work, but maybe:


var multilineString = String(current.variables.myLines);


var multilineArr = multilineString.split('\n');


// Do your loop


If it doesn't work, maybe log the value of multilineString or myLines and see how the data looks to determine how you can split it up properly.



I hope this helps a little.



Regards,



Mike


Hello Gareth,



Could you please log the value of myLines and post the result?



var mylines = current.variables.myLines;




gs.log("My lines are "   + mylines );




Thanks