javascript replace string with exact match

i have bulk of email address in textarea ‘[email protected]@[email protected]@[email protected]@hotmail.com’

what i want to do is add ‘comma space’ behind of .com / .com.my

expected result after onblur function in textarea: `[email protected], [email protected], [email protected], [email protected], [email protected], [email protected]

what i have tried.

b = value.split('.com.my').join('.com.my, ');
b = value.split('.com').join('.com, ');

problem : it will replace all ‘.com’ to ‘.com, ‘ even with ‘.com.my’

Answer

var value = "[email protected]@[email protected]@[email protected]@hotmail.com";
value = value.replace(/(.com(.my)?)/gm, '$1, ');
console.log(value.substring(0, value.length - 2));