Split Strings Into Array With Execute JavaScript regex

Document ID : KB000029054
Last Modified Date : 14/02/2018
Show Technical Document Details

This article offers an example of how you can split a string into a string array by:

  1. using a regular expression to split the string.
  2. splitting the string based on carriage return line feed characters (common on windows platforms).

To ensure that you are able to successfully verify the splitting of strings described below - the article includes steps that should be followed exactly as described below. If however, if you just want the short story of how to use regular expressions with the split String method then it would look like this: stringVar.split(/\r\n/);


Steps to test:

  1. Create a new flow.
  2. Add the "Run Command Line" action.
    • Set the following Inputs
      • Command Line String [String] field to be: net use
    • Set the following Outputs
      • Std Out Output [String] field to be an internal string parameter.
  3. Add the "Execute JavaScript code" action.
    • Set the following Inputs
      • Input1 [String] field set to be the internal string parameter used in step 3.
      • Open expression editor for the Script [String] field and add the following: arrOutput1 = input1.split(/\r\n/);
    • Set the following Outputs
      • Output1 [String[]] field to be an internal string array parameter.
  4. Add the "Arrays - Write Array to Text File" action.
    • Set the following Inputs
      • Array [Object[]] field to use the string array parameter used in Step 3's Output. 
      • Filename [String] field to use: C:\netoutsplit.txt
      • Create File If Not Exists [boolean] to use: true

Finally, save the flow and test it. When testing the flow you should be able to confirm that it worked successfully by checking the C:\netoutsplit.txt file. It should have all of the lines captured in the "net use" command output. The file is approximately six lines if there are no connections. 

In article TEC1920887 we will manipulate the array within the Execute JavaScript code action to remove empty/blank elements.