与该方法等效的 JavaScript 是什么C#:
C#
var x = "|f|oo||"; var y = x.Trim('|'); // "f|oo"
C#仅在字符串的 开头 和 结尾 处修剪所选字符!
一行就足够了:
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)); }