I have strings (painting title, painter name
) that have one or two commas, such as Big House, Jone Doe
or Big House, Blue, John Doe
. These are captions for the actual paintings. And I want to replace the correct comma with by
.
I can get the captions with
const captions = document.querySelectorAll('#gallery .caption');
for (const caption of captions) {
var new_caption = caption.textContent.toString();
If I use replace(","," by")
, that gets me the first comma. Then replace(",/g", " by")
does it for both. How do I replace just the second comma if there is one? Can't figure this out. Thanks.