JavaScript String Object Split method entry instance (used to split strings into groups) _ Basics

Source: Internet
Author: User

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:

A,b,c
, A,b,c

Please look carefully at the differences in the output of the two examples.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.