0
let str = "Testing, how to, remove, comma"

How can I remove that last comma (,) between "remove" and "comma" using JavaScript? Preference using replace with regex

1

1 Answer 1

1

There are probably better ways than using regular expressions, but you can do this:

str.replace(/,([^,]+$)/, "$1")

const str = "Testing, how to, remove, comma"

console.log(str.replace(/,([^,]+$)/, "$1"));

The regular expression matches a comma, then in a group it matches everything that is not a comma until the end of the string. The replacement is "$1", which is the first capturing group, meaning everything after the last comma.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.