なんのこっちゃという感じですが。
要は、
var string = "base=aaa\\|bbb\\=ccc|conf2dita_table_order=1|frame=bottom|platform=pla\\=foo\\|\\|\\|rrr\\=mmm|props=test"; var split = split_ex(string , "|"); // 結果 split = ["base=aaa", "bbb=ccc", "conf2dita_table_order=1", "frame=bottom", "platform=pla=foo", "rrr=mmm"]; var split = split_ex(string , "|"); // 第三引数を取ると、分割回数制限 split = ["base=aaa|bbb\=ccc", "conf2dita_table_order=1|frame=bottom|platform=pla=foo|||rrr=mmm|props=test"]
みたいな感じで、エスケープを意識したsplitです。
splitByChar = function() { var retval = [], targetstring = "", delimiter = "", limit = 0; if (typeof (arguments[0]) !== 'undefined') targetstring = arguments[0]; if (typeof (arguments[1]) !== 'undefined') delimiter = arguments[1]; if (typeof (arguments[2]) === 'number') limit = arguments[2]; var chara = "", nchara = ""; if (targetstring) { var escapeFlag = false, buf = "", counter = 0; for (var i = 0, j = targetstring.length; i < j; i++) { chara = targetstring.charAt(i); nchara = targetstring.charAt(i + 1); if (chara === "\\" && nchara === delimiter) { escapeFlag = true; } else if (escapeFlag === false && chara === delimiter) { if (buf.length > 0) { retval.push(buf); if (limit > 0 && ++counter >= limit) { var leftovers = targetstring.substring(i + 1).replace(/\\/g, ""); retval.push(leftovers); return retval; } } buf = ""; } else { buf += chara; escapeFlag = false; } } if (buf) { retval.push(buf); } } return retval; }