手動で行うと、誰かが再びこれに出くわした場合に備えて、これを思いつきました;)
splitAndUnescape = function(character, str, opt_escapeChar) {
var result = [];
var escapeChar = opt_escapeChar || '\\';
var tmp = '';
for (var index = 0; index < str.length; index++) {
var ch = str.charAt(index);
if (ch == escapeChar) {
ch = str.charAt(++index);
} else if (ch == character) {
result.push(tmp);
tmp = '';
continue;
}
tmp += ch;
}
if (tmp || result.length) {
result.push(tmp);
}
return result;
};
escapeAndJoin = function(character, array, opt_escapeChar) {
var escapeChar = opt_escapeChar || '\\';
var reEscape = new RegExp('(\\' + character + '|\\' + escapeChar + ')', 'g');
var out = [];
for (var index = 0; index < array.length; index++) {
out.push(array[index].replace(reEscape, escapeChar + '$1'));
}
return out.join(character);
};
概念実証:
serialized = escapeAndJoin(",", ["Hello", ",", "World"]); # -> "Hello,\,,World"
splitAndUnescape(",", serialized) # -> ["Hello", ",", "World"]