Struggling to craft a JavaScript function that eliminates the second instance of a character using regular expressions. Here is my current function:
var removeSecondOccurrence = function(string) {
return string.replace(/(.*)\1/gi, '');
}
The issue lies in its limitation to removing consecutive occurrences only. Ideally, I need it to also eliminate non-consecutive instances. For example, transforming 'papirana' into 'pairn'.
Your assistance would be greatly appreciated.