如何删除 javascript 中的最后一个逗号
How to remove the last comma in javascript
let str = "Testing, how to, remove, comma"
如何使用 JavaScript 删除“删除”和“逗号”之间的最后一个逗号 (,)?偏好使用替换为正则表达式
可能有比使用正则表达式更好的方法,但您可以这样做:
str.replace(/,([^,]+$)/, "$1")
const str = "Testing, how to, remove, comma"
console.log(str.replace(/,([^,]+$)/, ""));
正则表达式匹配一个逗号,然后在一个组中它匹配所有不是逗号的东西,直到字符串结束。替换为“$1”,这是第一个捕获组,表示最后一个逗号之后的所有内容。
let str = "Testing, how to, remove, comma"
如何使用 JavaScript 删除“删除”和“逗号”之间的最后一个逗号 (,)?偏好使用替换为正则表达式
可能有比使用正则表达式更好的方法,但您可以这样做:
str.replace(/,([^,]+$)/, "$1")
const str = "Testing, how to, remove, comma"
console.log(str.replace(/,([^,]+$)/, ""));
正则表达式匹配一个逗号,然后在一个组中它匹配所有不是逗号的东西,直到字符串结束。替换为“$1”,这是第一个捕获组,表示最后一个逗号之后的所有内容。