Reputation: 51
/* How to split the string and store to two variable only name and email id.*/
var str = "chris <[email protected]>"
/*expected output`enter code here`
name = "chris"
email_id = "[email protected]" */
Must remove the space and the ( "<>" )return the only string.
Upvotes: 1
Views: 71
Reputation: 150
You can use split()
the string, replace unwanted values from your string using replace()
var str = "chris <[email protected]>"
var splitedarr=str.split('<');
var name =splitedarr[0];
console.log("name :" + name)
var emailid =splitedarr[1].replace('>','')
console.log("email_id :" +emailid)
Upvotes: 1
Reputation: 445
you can use regular expression to remove unwanted characters inside a string and assign it to two different variables as below
var str = "chris <[email protected]>"
const [name,email] = str.split(/<|>/g);
Upvotes: 3
Reputation: 521053
I would split the input on the single space occurring after the name and before the email address. Then, take a substring to isolate the email address.
var str = "chris <[email protected]>";
var parts = str.split(/[ ](?=<)/);
var name = parts[0];
var email = parts[1].substring(1, parts[1].length - 1);
console.log("name = " + name);
console.log("email_id = " + email);
Upvotes: 0