输入 :my.first.last.email@example.computer.com
结果 :my.first.last.email&&example##computer##com
解决方案1:
var mystring = "my.first.last.email@example.computer.com";
//replace '.' after '@' with '##', then replace '@' with '&&'.
var result = mystring.replace(/(?!.*@)\./g, "##").replace(/@/, "&&");
document.write(result);
解决方案2(可配置):
var mystring = "my.email@computer.com";
var replacements = {
'@' : '&&',
'.' : '##'
};
var str = "my.first.last.email@example.computer.com";
//match latter part of the string
var result = str.replace(/@\w+(\.\w+)+/g, function(at_and_after) {
//replace all '.' and '@' in that part.
return at_and_after.replace(/@|\./g, function(m) { return replacements[m]});
});
document.write(result); //console.log(result) or alert(result) is a better way for demo