I need to replace the last instance of a comma with and. I have tried this:
myString = myString.replace('/_([^,]*)$/','and$1');
But the string is not affected.
Any ideas?
You have a _ instead of a , and you've wrapped your regex in quotes. I think you'll also need to add a space before the and:
myString = myString.replace(/,([^,]*)$/,'\ and$1');
Edit:
You could also do this without regex, if you're so inclined:
str = "Maria, David, Charles, Natalie";
lastComma = str.lastIndexOf(',');
newStr = str.substring(0, lastComma) + ' and' + str.substring(lastComma + 1);
//=> "Maria, David, Charles and Natalie"
replace(/_([^,]*)$/,'and$1'). The/ ... /marks the start and end of the regex.