In IE, "x".split(/(x)/).length
returns 0
In Firefox, Chrome, Safari, and Opera, it returns 3
.
Does anybody know the reason why? If possible, a reference link will be greatly appreciated.
I believe that it is a IE regex implementation issue, but I can't find any document about that.
The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.
To split a string by multiple spaces, call the split() method, passing it a regular expression, e.g. str. trim(). split(/\s+/) . The regular expression will split the string on one or more spaces and return an array containing the substrings.
Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.
You're correct that there are implementation issues. IE both ignores empty values and capture blocks within regular expressions.
So for
"foo".split(/o/)
IE gives
[f]
where the other browsers give
["f","",""]
and when you add the capturing:
"foo".split(/(o)/)
IE performs the same, but the others add the captured delimiter to the resulting array to give
["f","o","","o",""]
So unfortunately you probably either need to avoid using split, or code around these issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With