I'm looking to replace the second last occurrence of a character in a given string. The length of the strings may vary but the delimiter is always the same. Here are some examples along with my attempted solutions:
Input 1: james.sam.uri.stackoverflow.com
Output 1: [email protected]
Input 2: noman.stackoverflow.com
Output 2: [email protected]
Input 3: queen.elizabeth.empire.co.uk
Output 3: [email protected]
My approach:
//Although this solution works, I am looking for a regex solution
const e = "noman.stackoverflow.com"
var index = e.lastIndexOf(".", email.lastIndexOf(".")-1)
return ${e.substring(0,index)}@${e.substring(index+1)}
Regex:
e.replace(/\.(\.*)/, @$1)
//This regex solution only works for Input 2 and not Input 1. I need a regex that can handle both cases, as it currently only matches the first dot.