javascript 修剪弦

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 修剪弦相关的知识,希望对你有一定的参考价值。

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, "\\"));

以上是关于javascript 修剪弦的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript JavaScript修剪字符串

JavaScript Javascript修剪LTrim和RTrim函数

如何在javascript中验证之前修剪非必填字段

JavaScript 将修剪函数添加到Javascript String Object

如何在Javascript中将字符串修剪为N个字符?

javascript 修剪额外斜线的路径