修剪字符串中的特定字符

修剪字符串中的特定字符,第1张

修剪字符中的特定字符

一行就足够了:

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));}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5640999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存