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