Usando JavaScript, como posso remover a última vírgula, mas apenas se a vírgula for o último caractere ou se houver apenas espaço em branco após a vírgula? Este é o meu código. Eu tenho um violino de trabalho . Mas tem um bug.
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;
}