一行就足够了:
var x = '|f|oo||';var y = x.replace(/^|+||+$/g, '');document.write(x + '<br />' + y);^|+ beginning of the string, pipe, one or more times| or|+$ pipe, one or more times, end of the string
在功能上:
function trim (s, c) { if (c === "]") c = "\]"; if (c === "\") c = "\"; return s.replace(new RegExp( "^[" + c + "]+|[" + c + "]+$", "g" ), "");}chars = ".|]\";for (c of chars) { s = c + "foo" + c + c + "oo" + c + c + c; console.log(s, "->", trim(s, c));}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)