Używając JavaScript, jak mogę usunąć ostatni przecinek, ale tylko wtedy, gdy przecinek jest ostatnim znakiem lub jeśli po przecinku jest tylko spacja? To jest mój kod. Mam działające skrzypce . Ale ma błąd.
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;
}