JavaScript split method
The split method is used to split a string into a string array and return the array. The syntax is as follows:
The code copy is as follows:
str_object.split(separator, limit)
Parameter description:
| parameter | illustrate |
|---|---|
| str_object | String (object) to operate |
| separator | Required. Delimiter, string or regular expression, split str_object from where this parameter specifies |
| limit | Optional. Specifies the maximum length of the returned array. If this parameter is set, the returned substrings will not be more than the array specified by this parameter. If this parameter is omitted, all compliance rules will be divided |
Tip: If an empty string ("") is used as a separator, each character in str_object will be divided, as shown in the following example.
split method example
The code copy is as follows:
<script language="JavaScript">
var str = "www.VeVB.COM";
document.write( str.split(".") + "<br />" );
document.write( str.split("") + "<br />" );
document.write(str.split(".", 2));
</script>
Run this example and output:
The code copy is as follows:
www,jb51,net
w,w,w,.,j,b,5,1,.,n,e,t
www,jb51
Tip: As shown in the above example, if an empty string ("") is used as a separator, each character in str_object will be split between.
The split method uses regular expressions
The split method also supports splitting strings using regular expressions:
The code copy is as follows:
<script language="JavaScript">
document.write( "1a2b3c".split(//d/) + "<br />");
document.write( ":a:b:c".split(":") );
</script>
Run this example and output:
The code copy is as follows:
a, b, c
,a,b,c
Please carefully observe the differences in the outputs of the two examples.