Created
May 13, 2015 19:45
-
-
Save victorono/b2b007f1b032ca3abc9f to your computer and use it in GitHub Desktop.
Override Internet Explorer implementation of String.split
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * String.split(delimiter,limit) | |
| * Override Internet Explorer implementation of String.split to handle regular | |
| * expression capture groups in the same manner as all other browsers. Each | |
| * capture group in the matched text will be included in the split array. | |
| */ | |
| if("a'b".split(/(')/g).length==2){ | |
| String.prototype.split = function(delimiter/*,limit*/){ | |
| if(typeof(delimiter)=="undefined"){ | |
| return [this]; | |
| } | |
| var limit = arguments.length>1?arguments[1]:-1; | |
| var result = []; | |
| if(delimiter.constructor==RegExp){ | |
| delimiter.global = true; | |
| var regexpResult,str=this,previousIndex = 0; | |
| while(regexpResult = delimiter.exec(str)){ | |
| result.push(str.substring(previousIndex,regexpResult.index)) | |
| for (var captureGroup=1,ct=regexpResult.length;captureGroup<ct;captureGroup++){ | |
| result.push(regexpResult[captureGroup]); | |
| } | |
| str=str.substring(delimiter.lastIndex,str.length); | |
| } | |
| result.push(str); | |
| }else{ | |
| var searchIndex=0,foundIndex=0,len=this.length,dlen=delimiter.length; | |
| while(foundIndex!=-1){ | |
| foundIndex = this.indexOf(delimiter,searchIndex); | |
| if(foundIndex!=-1){ | |
| result.push(this.substring(searchIndex,foundIndex)) | |
| searchIndex = foundIndex+dlen; | |
| }else{ | |
| result.push(this.substring(searchIndex,len)); | |
| } | |
| } | |
| } | |
| if(arguments.length>1&&result.length>limit){ | |
| result = result.slice(0,limit); | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment