Skip to content

Instantly share code, notes, and snippets.

@cjcliffe
Created September 16, 2011 03:00
Show Gist options
  • Save cjcliffe/1221092 to your computer and use it in GitHub Desktop.
Save cjcliffe/1221092 to your computer and use it in GitHub Desktop.
Split string into array by multiple characters, useful for script parsing.
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