การใช้ 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;
}