let text = 'This is a text with multiple words'; let words = text.split(' '); console.log('Words: ', words);
split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.
When found, separator is removed from the string, and the substrings are returned in an array.
If separator is a regular expression with capturing parentheses, then each time separator matches, the results (including any undefined results) of the capturing parentheses are spliced into the output array.
If the separator is an array, then that Array is coerced to a String and used as a separator.
- Split a string into words