The split() method splits a string into a string array according to the specified delimiter. Usage: str.split(separator, limit), separator is the separator, and limit is the maximum length of the returned array.
The split() method in JavaScript
Question: The split() method in JavaScript has What does it do?
Answer:
The split() method is used to split a string into a string array according to the specified delimiter.
Usage:
<code>str.split(separator, limit)</code>
Return results:
The split() method returns an array containing split string elements. If limit is less than the number of occurrences of the delimiter in the string, the array contains only limit elements.
Example:
<code>const str = "Hello, world!"; // 按空格拆分 const arr1 = str.split(" "); // ["Hello,", "world!"] // 按逗号拆分 const arr2 = str.split(","); // ["Hello", " world!"] // 按正则表达式拆分 const arr3 = str.split(/\s+/, 2); // ["Hello", "world!"]</code>
Note: The
The above is the detailed content of How to use splite in js. For more information, please follow other related articles on the PHP Chinese website!