1

I am a beginner, how do I combine them:

var mystring = "[email protected]";
document.write(mystring.replace(/@/, "&&"));

prints my.email&&computer.com

var mystring = "[email protected]";
document.write(mystring.replace(/\./, "##"));

prints my##[email protected]

I have two questions: How do I make this regex (mystring.replace(/./, "##") to after @ change the dot to ## and how can I combine those two lines into one, and final read is:my.email&&computer##com

4
  • 3
    mystring.replace(/@/, "&&").replace(/\./, "##") :) Commented Feb 7, 2013 at 3:52
  • 2
    hold on, you expect result is my.email&&computer##com instead of my##email&&computer##com ? Commented Feb 7, 2013 at 3:57
  • @Rain Diao yes, need this my.email&&computer##com Commented Feb 7, 2013 at 4:00
  • See the single replace solution too... Commented Feb 7, 2013 at 4:34

5 Answers 5

4

input : [email protected]

result : my.first.last.email&&example##computer##com

Solution 1:

var mystring = "[email protected]";
//replace '.' after '@' with '##', then replace '@' with '&&'.
var result = mystring.replace(/(?!.*@)\./g, "##").replace(/@/, "&&");
document.write(result);

Solution 2 (configurable):

var mystring = "[email protected]";

var replacements = {
                    '@' : '&&',
                    '.' : '##'
                   };

var str =  "[email protected]";

//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
Sign up to request clarification or add additional context in comments.

8 Comments

thanks but it prints my##email&&computer.com, I'd like it to print my.email&&computer##com
thanks again, Uncaught SyntaxError: Unexpected token ; on line 15
Many thanks @Rain Diao, it prints my##email&&computer##com, I guess the regex needs to be like, do nothing before @, after @ then replace .
he used /g , it means all . will be replaced with ##
yes, but remove /g, it doesnt replace the last ., just needed to replace the last .
|
2

Try this...

var mystring = "[email protected]";
document.write(mystring.replace(/(.*@.*)\./, "$1##").replace(/@/, "&&"));

2 Comments

Updated answer to skip replacing dot before @.
@olo Remember, this only replaces a single occurrence of @ and dot.
1

You could use split to split the string at /@/ and apply the second regexp to the second part of the string, then join the results back together with &&.

Comments

1

This should work:

var mystring = "[email protected]";
document.write(mystring.replace(/(.*?)(@)(.*?)(\.)(.*)/, "$1&&$3##$5"));

Result:

my.email&&computer##com

See it here working: http://jsfiddle.net/gnB85/

1 Comment

@olo - I didn't noticed that you wanted to replace the "@" by two "&" ... It's fixed now. You get "&&" instead of "@" in a single regex replace
0

Try this:

var mystring = "[email protected]"
document.write(mystring.replace(/\.(?!\w+@)/, '##').replace(/@/, '&&'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.