I have a line that is generated, and is essentially a list of things. This line is what the user will read, so I'm trying to create it well. I separate the generated list with commas and spaces:
(a+'').replace(/,/g, ", ");
produces
1, 2, 3, 4
However, I would like to change the last comma to ", and" so that it reads
1, 2, 3, and 4
I tried the following:
((a+'').replace(/,/g, ", ")).replace(/,$/, ", and");
but it does not work, and I think, because it is only a search for commas at the end of the line, and not the last comma in the line, right?
Also, if there are only 2 elements in a line, I want the comma to be replaced simply with "and", and not with "and", to make more sense grammatically.
How can I achieve what I am looking for?
Gtwok source share