AvailabilityJavaScript 1.1; JScript 3.0; ECMAScript v1; enhanced in ECMAScript v3 Synopsisstring.split(delimiter, limit) Arguments
ReturnsAn array of strings, created by splitting string into substrings at the boundaries specified by delimiter. The substrings in the returned array do not include delimiter itself, except in the case noted below. DescriptionThe split( ) method creates and returns an array of as many as limit substrings of the specified string. These substrings are created by searching the string from start to end for text that matches delimiter and breaking the string before and after that matching text. The delimiting text is not included in any of the returned substrings, except as noted below. Note that if the delimiter matches the beginning of the string, the first element of the returned array will be an empty string -- the text that appears before the delimiter. Similarly, if the delimiter matches the end of the string, the last element of the array (assuming no conflicting limit) will be the empty string. If no delimiter is specified, the string is not split at all, and the returned array contains only a single, unbroken string element. If delimiter is the empty string or a regular expression that matches the empty string, the string is broken between each character, and the returned array has the same length as the string does, assuming no smaller limit is specified. (Note that this is a special case since the empty string before the first character and after the last character is not matched.) We said above that the substrings in the array returned by this method do not contain the delimiting text used to split the string. However, if delimiter is a regular expression that contains parenthesized subexpressions, the substrings that match those parenthesized subexpressions (but not the text that matches the regular expression as a whole) are included in the returned array. Note that the String.split( ) method is the inverse of the Array.join( ) method. ExampleThe split( ) method is most useful when you are working with highly structured strings. For example: "1:2:3:4:5".split(":"); // Returns ["1","2","3","4","5"] "|a|b|c|".split("|"); // Returns ["", "a", "b", "c", ""] Another common use of the split( ) method is to parse commands and similar strings by breaking them down into words delimited by spaces: var words = sentence.split(' '); See the Section section for details on a special case when delimiter is a single space. It is easier to split a string into words using a regular expression as a delimiter: var words = sentence.split(/\s+/); To split a string into an array of characters, use the empty string as the delimiter. Use the limit argument if you only want to split a prefix of the string into an array of characters: "hello".split(""); // Returns ["h","e","l","l","o"] "hello".split("", 3); // Returns ["h","e","l"] If you want the delimiters, or one or more portions of the delimiter included in the returned array, use a regular expression with parenthesized subexpressions. For example, the following code breaks a string at HTML tags and includes those tags in the returned array: var text = "hello <b>world</b>"; text.split(/(<[^>]*>)/); // Returns ["hello ","<b>","world","</b>",""] BugsIn Netscape's implementations of JavaScript, when language Version 1.2 is explicitly requested (with the language attribute of a <script> tag, for example), the split( ) method has one special-case behavior: if delimiter is a single space, the method splits the string at spaces but ignores any white space at the beginning and end of the string. See Section 11.6, for further details. See AlsoArray.join( ), RegExp; Chapter 10 |