Sử dụng JavaScript, làm cách nào tôi có thể xóa dấu phẩy cuối cùng, nhưng chỉ khi dấu phẩy là ký tự cuối cùng hoặc nếu chỉ có khoảng trắng sau dấu phẩy? Đây là mã của tôi. Tôi có một fiddle làm việc . Nhưng nó có một lỗi.
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;
}