JavaScript: replace last occurrence of text in a string

Well, if the string really ends with the pattern, you could do this:

str = str.replace(new RegExp(list[i] + '$'), 'finish');

Leave a Comment