使用 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;
}