The email address is dynamic and The output should be:
Email Address:
Output should be:
sample
@email.com
Can JavaScript split the inputed email address?
Thank you guys!
4 Answers 4
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
jonny
3,1291 gold badge21 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Dmitry Lobov
3031 silver badge10 bronze badges
Comments
The easiest way
const email='[email protected]'
console.log(email.split('@')[0]);
Comments
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
Comments
lang-js
"[email protected]".split("@")-- >["sample", "email.com"]!