JavaScript's split() method splits a string into an array, bounded by delimiters. The syntax is: string.split(separator). The separator parameter can be a character, string, or regular expression. The return value is an array containing the split substrings.
##split Usage of method in JavaScript
split( ) method is used to split a string into an array of substrings. It takes a delimiter as a parameter and extracts the substring after each delimiter in the string.
Syntax:
<code>string.split(separator)</code>
Parameters:
: used to separate characters String delimiter. Can be a character, string, or regular expression.
Return value:
An array containing split substrings.Example:
<code>const str = "Hello, world!"; // 以逗号分隔 const arr1 = str.split(','); // ['Hello', ' world!'] // 以空格分隔 const arr2 = str.split(' '); // ['Hello', ',', 'world', '!'] // 以正则表达式分隔 const arr3 = str.split(/\s+/); // ['Hello', 'world!']</code>
Other usage:
method.
<code>const arr4 = str.split(' ', 2); // ['Hello', 'world!']</code>
<code>const arr5 = str.split(/\s*(,|\s)\s*/); // ['Hello', '', 'world!']</code>
<code>const arr6 = str.split(''); // ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']</code>
The above is the detailed content of How to use split in js. For more information, please follow other related articles on the PHP Chinese website!