Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anybody know why "x".split(/(x)/).length returns 0 in IE?

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.

like image 316
YOU Avatar asked Dec 18 '09 13:12

YOU


People also ask

What does the split method return?

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.

Does split return a string?

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.

How do I split a string into multiple spaces?

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.

How do I split a string with multiple separators in angular?

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.


1 Answers

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.

like image 105
Shaun Avatar answered Nov 03 '22 06:11

Shaun