The split method is used in JavaScript to split a string into a string array. The syntax is "string.split (string or regular expression, returns the maximum length of the array)"; if not specified, return length of the array, the entire string will be split, and the return result of this method is a string array.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
The split() method is used to split a string into a string array.
Tip: If you use the empty string ("") as a separator, each character in the stringObject will be split.
Note: The split() method does not change the original string.
Syntax
string.split(separator,limit)
Parameter Description
separator Optional. A string or regular expression to split the string Object from where specified by this parameter.
limit Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.
Return value
Array A string array. The array is created by splitting the string string Object into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> </head> <body> <p id="demo">单击按钮显示分割后的数组</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="How are you doing today?"; var n=str.split(" ",3); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
Output result:
[Related recommendations: javascript video tutorial、web front-end】
The above is the detailed content of How to use split method in javascript. For more information, please follow other related articles on the PHP Chinese website!