JavaScript Split method
The split method is used to split a string into an array of strings and return the array. The syntax is as follows:
Copy Code code as follows:
Str_object.split (separator, limit)
Parameter description:
Parameters |
Description |
Str_object |
The string to manipulate (object) |
Separator |
Necessary. Delimiter, string, or regular expression, separated from the place specified by the parameter Str_object |
Limit |
Optional. Specifies the maximum length of the returned array. If this argument is set, the returned substring will not be more than the array specified by this parameter. If this argument is omitted, the conforming rule will be split |
Tip: If you use an empty string ("") as each character in the Separator,str_object, it will be split, as shown in the following example.
Split Method Instance
Copy Code code as follows:
<script language= "JavaScript" >
var str = "Www.jb51.net";
document.write (Str.split (".") + "<br/>");
document.write (Str.split ("") + "<br/>");
document.write (Str.split (".", 2));
</script>
Run the example and output:
Copy Code code as follows:
Www,jb51,net
W,w,w,., j,b,5,1,., n,e,t
Www,jb51
Tip: As shown in the example above, if you use an empty string ("") as each character in Separator,str_object, it will be split.
The split method uses regular expressions
The split method also supports the use of regular expressions to segment strings:
Copy Code code as follows:
<script language= "JavaScript" >
document.write ("1a2b3c". Split (/\d/) + "<br/>");
document.write (": A:b:c". Split (":"));
</script>
Run the example and output:
Copy Code code as follows:
Please look carefully at the differences in the output of the two examples.