Contact Free trial Login

splitBy

splitBy(String, Regex): Array<String>

Splits a string based on a regex.

The separator can match any character in the input. Note that splitBy performs the opposite operation of joinBy.

Parameters

Name Description

text

The string to split.

regex

A regular expression used to separate string. If it does not match some part of the string, the function will return the string unseparated in an array.

Example

This example uses the regular expression \/^*.b./\ to find and use -b- as a separator. Notice that the separator is omitted from the output.

Source

%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy(/^*.b./) }

Output

{
  "split": ["a","c"]
}

splitBy(String, String): Array<String>

Splits a string based on a separator.

The separator can match any character in the input. Note that splitBy performs the opposite operation of joinBy.

Parameters

Name Description

text

The string to split.

separator

A string used to separate the input string. It must match some part of the string.

Example

This example uses the hyphen (-) as the separator. Note that the selector is not retained in the output.

Source

%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy("-") }

Output

{
  "split": ["a", "b", "c"]
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub