Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - how to split the give string in expected output

/* 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.

question from:https://stackoverflow.com/questions/65839753/how-to-split-the-give-string-in-expected-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...