Skip to content

Instantly share code, notes, and snippets.

@timmarinin
Last active September 2, 2016 16:58
Show Gist options
  • Save timmarinin/1733133b02696f87c64224916d1c52ba to your computer and use it in GitHub Desktop.
Save timmarinin/1733133b02696f87c64224916d1c52ba to your computer and use it in GitHub Desktop.
function groupByUndefined(col) {
var ret = []
var current = []
for (var i = 0; i < col.length; i++) {
if (typeof col[i] !== 'undefined') current.push(col[i])
else {
ret.push(current)
current = []
}
}
ret.push(current)
return ret
}
function splitByComma(txt) {
var matches = txt.split(/(\(.*?\))|,/)
return groupByUndefined(matches).map(x => x.join('')).filter(x=>x)
}
// example:
splitByComma('some text, like this, is (inevitable, though) entered by, who\'d guess it, user!')
// becomes ["some text", " like this", " is (inevitable, though)", " entered by", " who'd guess it", " user!"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment