Utilizzando JavaScript, come posso rimuovere l'ultima virgola, ma solo se la virgola è l'ultimo carattere o se c'è solo uno spazio bianco dopo la virgola? Questo è il mio codice Ho un violino funzionante . Ma ha un 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;
}