Skip to content

Instantly share code, notes, and snippets.

@jwerle
Last active December 28, 2015 17:09
Show Gist options
  • Save jwerle/7533762 to your computer and use it in GitHub Desktop.
Save jwerle/7533762 to your computer and use it in GitHub Desktop.
function p (s, b, e, d) {
var c = null;
var i = 0;
var n = 0;
var r = [];
var x = 0;
var y = 0;
while (null != (c = s[i++])) {
// got first brace
if (b == c && 0 == x) {
x = 1;
r[n] = ""; // empty string for token
continue;
}
// found token delimiter
// and setting token `y' flag
// to 1
if (1 == x && d == c) {
y = 1;
continue;
}
// in brace and
// found first token
// after brace
if (1 == x && 0 == y && d != c) {
r[n] += c;
continue;
}
// found end brace
// and first brace found
// and delimiter set
if (e == c && 1 == x && 1 == y) {
n++;
x = 0;
y = 0;
}
}
return r;
}
console.log(p(
"[foo; after] bla hello [bar; after] biz baz omfg [yo; balha sa sas ah] dfd"
, '['
, ']'
, ';'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment