0

The email address is dynamic and The output should be:

Email Address:

[email protected]

Output should be:

sample
@email.com

Can JavaScript split the inputed email address?

Thank you guys!

asked Aug 20, 2015 at 16:57
2

4 Answers 4

2

Yes, using the split method:

var str = "[email protected]";
var res = str.split("@"); //An array, which looks like this [sample, email.com]
answered Aug 20, 2015 at 17:00
Sign up to request clarification or add additional context in comments.

Comments

1
var email = [email protected]
// variant 1 (without '@')
var mailArr = email.split('@');
var logn = mailArr[0];
var mailHost = mailArr[1];
// variant 2
var atPosition = email.indexOf('@');
var logn = email.slice(0, atPosition);
var mailHost = email.slice(atPosition, -1);

And for more common uses you should use regular expressions

answered Aug 20, 2015 at 17:06

Comments

1

The easiest way

const email='[email protected]'
console.log(email.split('@')[0]);

answered Feb 2, 2023 at 22:49

Comments

0

we can split using split method

var email = "[email protected]";
var arry = email.split("@");
console.log(arry[0]) //demo
console.log(arry[1]) //gmail.com 
answered Aug 20, 2015 at 17:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.