Skip to content

Instantly share code, notes, and snippets.

@tomasdev
Created December 20, 2011 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasdev/1500176 to your computer and use it in GitHub Desktop.
Save tomasdev/1500176 to your computer and use it in GitHub Desktop.
String multiple split
String.prototype.splitAll = function(){
var i = arguments.length,
original = this,
str = arguments[--i];
for( ; str ; ){
original = original.split(str);
(str = arguments[--i]) && (original = original.join(str));
}
return original;
}
@mathiasbynens
Copy link

You know String#split() accepts a regex, right? So instead of 'foo1bar2baz'.splitAll('1', '2') you could just do 'foo1bar2baz'.split(/1|2/g). Is there a use case that I’m missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment