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