headerphoto

Method: split()

split() splits the input string into an array of strings, each of which is the sub-string separating the patterns matched. It works very similarly to JavaScript's String.split(separator) function when separator is a regular expression.

Syntax

var exp = new apgExp(pattern[, flags]);
var result = exp.split(str[, limit]);

Parameters

  • str: string: The string to match patterns in.
  • limit: integer > 0: default: Infinity: The maximum number of matches to find.

Return

Returns an array of strings. The modes or flags parameters are ignored. An attempt is made to match the pattern in the input string up to and including limit times.

  • str is empty, null or undefined: The array contains a single, empty string.
  • pattern matches the entire input string, str: The array contains a single, empty string.
  • pattern does not match: The array contains a single string, a copy of the original string.
  • pattern finds matches multiple times: The array contains multiple strings, each being a sub-string of the input string—the prefix, separator and suffix sub-strings, if any.
  • pattern is an empty string "": The array contains one string each for each character in the input string. In this regard it acts like JavaScript's String.split("").

Example 1

Split the string at the semicolons, spaces optional.

var pattern, exp, str, result;
pattern  = 'pattern = owsp ";" owsp\n';
pattern += 'owsp    = *%d32\n';
str = "one;two ;three ; four";
exp = new apgExp(pattern);
result = exp.split(str);
console.log("   str: " + str);
console.log("result: " + result);
/ * result */
   str: one;two ;three ; four
result: one,two,three,four

Example 2

Split the string into all of its constituent characters.

var pattern, exp, str, result;
pattern  = 'pattern = ""\n';
str = "one;two;three;four";
exp = new apgExp(pattern);
result = exp.split(str);
console.log("   str: " + str);
console.log("result: " + result);
/ * result */
   str: one;two;three;four
result: o,n,e,;,t,w,o,;,t,h,r,e,e,;,f,o,u,r