Created
August 14, 2013 14:00
-
-
Save FernandoVezzali/6231325 to your computer and use it in GitHub Desktop.
replaceAll function for JavaScript
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.prototype.replaceAll = function( token, newToken, ignoreCase ) { | |
var _token; | |
var str = this + ""; | |
var i = -1; | |
if ( typeof token === "string" ) { | |
if ( ignoreCase ) { | |
_token = token.toLowerCase(); | |
while( ( | |
i = str.toLowerCase().indexOf( | |
token, i >= 0 ? i + newToken.length : 0 | |
) ) !== -1 | |
) { | |
str = str.substring( 0, i ) + | |
newToken + | |
str.substring( i + token.length ); | |
} | |
} else { | |
return this.split( token ).join( newToken ); | |
} | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment