This article mainly introduces an introductory example of the split method of JavaScript string objects. The split method is used to split a string into an array. Friends in need can refer to
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:
str_object.split(separator, limit)
Parameter description:
参数 | 说明 |
---|---|
str_object | 要操作的字符串(对象) |
separator | 必需。分隔符,字符串或正则表达式,从该参数指定的地方分割 str_object |
limit | 可选。指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果省略该参数,则符合规则都将被分割 |
Tip: If an empty string ("") is used as a separator, each character in str_object will be separated by Split, as shown in the example below.
split method instance
<script language="JavaScript"> var str = "www.php.cn"; document.write( str.split(".") + "<br />" ); document.write( str.split("") + "<br />" ); document.write(str.split(".", 2)); </script>
Run this example, the output is:
www,php,cn
w,w,w, .,p,h,p,.,c,n
www,php
Tip: As shown in the above example, if the empty string ("") is used as separator, str_object will be separated between each character.
split method uses regular expressions
The split method also supports using regular expressions to split strings:
<script language="JavaScript"> document.write( "1a2b3c".split(/\d/) + "<br />"); document.write( ":a:b:c".split(":") ); </script>
Run the example , Output:
a,b,c
,a,b,c
Please carefully observe the difference in the output of the two examples.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone’s study. For more related tutorials, please visit JavaScript Video Tutorial!