使用 JavaScript,如何删除最后一个逗号,但只有在逗号是最后一个字符或逗号后面只有空格的情况下才能删除?这是我的原则。 我有个能用的 小提琴但它有个窃听器。
var str = 'This, is a test.';
alert( removeLastComma(str) ); // should remain unchanged
var str = 'This, is a test,';
alert( removeLastComma(str) ); // should remove the last comma
var str = 'This is a test, ';
alert( removeLastComma(str) ); // should remove the last comma
function removeLastComma(strng){
var n=strng.lastIndexOf(",");
var a=strng.substring(0,n)
return a;
}