function trim (s, c) {
if (c === "]") c = "\\]";
if (c === "\\") c = "\\\\";
return s.replace(new RegExp(
"^[" + c + "]+|[" + c + "]+$", "g"
), "");
}
s = ".foo..oo...";
console.log(s, "->", trim(s, "."));
s = "|foo||oo|||";
console.log(s, "->", trim(s, "|"));
s = "]foo]]oo]]]";
console.log(s, "->", trim(s, "]"));
s = "\\foo\\\\oo\\\\\\";
console.log(s, "->", trim(s, "\\"));