Created
July 28, 2015 18:50
-
-
Save christophemarois/0204ccc1ad6388cffd8c to your computer and use it in GitHub Desktop.
Split a string with multiple regular expressions, then flatten the results
This file contains 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
// var splitResults = multisplit(str, regExp1[, regExp2[, ...[, regExpN]]]) | |
// returns single-leveled array of split results | |
var multiSplit = function (str) { | |
var regexps = [].slice.call(arguments).slice(1); | |
var parts = [str]; | |
while (regexps.length) { | |
parts = parts.map(function(part){ return part.split(regexps[0]); }); | |
parts = Array.prototype.concat.apply([], parts); | |
regexps.shift(); | |
} | |
return parts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment