I have a text
test, text, 123, without last comma
I need it to be
test, text, 123 without last comma
(no comma after 123). How to achieve this using JavaScript?
str.replace(/,(?=[^,]*$)/, '')
This uses a positive lookahead assertion to replace a comma followed only by non-commata.
A non-regex option:
var str = "test, text, 123, without last comma";
var index = str.lastIndexOf(",");
str = str.substring(0, index) + str.substring(index + 1);
But I like the regex one. :-)