Created
September 16, 2011 03:00
-
-
Save cjcliffe/1221092 to your computer and use it in GitHub Desktop.
Split string into array by multiple characters, useful for script parsing.
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
function multiSplit(split_str, split_chars) { | |
var arr = split_str.split(split_chars[0]); | |
for (var i = 1, iMax = split_chars.length; i < iMax; i++) { | |
var sc = split_chars[i]; | |
for (var j = 0, jMax = arr.length; j < jMax; j++) { | |
var arsplit = arr[j].split(sc); | |
if (arsplit.length > 1) { | |
for (var k = 0; k < arsplit.length; k++) { | |
if (arsplit[k].trim() !== "") { | |
arr.splice(j + k, (k == 0) ? 1 : 0, arsplit[k]); | |
iMax++; | |
} | |
} | |
} else { | |
arr[j] = arr[j].trim().replace(sc, ""); | |
if (arr[j] === "") { | |
arr.splice(j, 1); | |
jMax--; | |
j--; | |
} | |
} | |
} | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment