[英]Remove multiple commas from start and end of string
考虑这种情况
var str1 = '^^ranbir$$this is first comment,,';
var str2 = ',,^^check$$this is another comment,,';
var str3 = ',,^^mike$$this is 3rd comment, but this is not the last one,,';
我希望各自的输出是
console.log(str1) // ^^ranbir$$this is first comment
console.log(str2) // ^^check$$this is another comment
console.log(str3) // ^^mike$$this is 3rd comment, but this is not the last one
基本上删除字符串开头和结尾的所有逗号。 通过尝试堆栈溢出中的几个解决方案,我只能从字符串的开头和结尾删除一个逗号,但无法使其正常工作。
用于删除字符串开头和结尾的尾部逗号和可能空格的统一解决方案:
var str3 = ',,, ^^mike$$this is 3rd comment, but this is not the last one ,,,, ', replaced = str3.replace(/^\\s*,+\\s*|\\s*,+\\s*$/g, ''); console.log(replaced);
您可以匹配逗号之间的部分:
const re = /^,*(.*?),*$/; const data = ',,,^^ranbir$$this is first comment,,'; const result = data.match(re); console.log(result[1]);
var edited = test.replace(/^,|,$/g,'');
^,匹配字符串开头的逗号,, $匹配字符串结尾的逗号。
伪代码:foo包含(foo.replace(/ ^,|,$ / g,''));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.