我是初学者,如何将它们结合起来:
var mystring = "my.email@computer.com";
document.write(mystring.replace(/@/, "&&"));
打印my.email&&computer.com
var mystring = "my.email@computer.com";
document.write(mystring.replace(/\./, "##"));
打印my##email@computer.com
我有两个问题:
如何将此正则表达式(mystring.replace(/./,“##”)设置为@
后将dot
更改为##
,如何将这两行合并为一个最终阅读是:my.email&&computer##com
答案 0 :(得分:4)
输入: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
答案 1 :(得分:2)
试试这个......
var mystring = "my.email@computer.com";
document.write(mystring.replace(/(.*@.*)\./, "$1##").replace(/@/, "&&"));
答案 2 :(得分:1)
您可以使用split将字符串拆分为/ @ /并将第二个正则表达式应用于字符串的第二部分,然后将结果与&&连接起来。
答案 3 :(得分:1)
这应该有效:
var mystring = "my.email@computer.com";
document.write(mystring.replace(/(.*?)(@)(.*?)(\.)(.*)/, "$1&&$3##$5"));
结果:
my.email&&computer##com
在此处查看工作:http://jsfiddle.net/gnB85/
答案 4 :(得分:0)
试试这个:
var mystring = "my.email@computer.com"
document.write(mystring.replace(/\.(?!\w+@)/, '##').replace(/@/, '&&'));