用@符号按字符串分割数组对象

时间:2019-07-18 14:49:24

标签: javascript arrays node.js

我有一个流动的数组对象

let old_array = {
 "valone":  "facebook",
 "notification": "new message! @user1@example.com @user2@example.com"
}

我想让所有带有@符号的用户进入波纹管这样的新数组

let new_array = {"user1@example.com", "user2@example.com"}

任何想法如何做到这一点,我有用户数组拆分即时通讯只能接收@符号的电子邮件。

let new_array = old_array .split("@");

3 个答案:

答案 0 :(得分:3)

尝试一下:

let old_array = {
 "valone":  "facebook",
 "notification": "new message! @user1@example.com @user2@example.com"
}

const result =  old_array.notification.match(/(?<=@)(\w*@\w*.\w*)/g);

console.log(result);

答案 1 :(得分:0)

您必须在old_array中拆分字符串。试试这个:

let old_array = {
  "valone":  "facebook",
  "notification": "new message! @user1@example.com @user2@example.com"
}
    
let notification = old_array["notification"]; //Get notification-property
    
let email_string = notification.substring(notification.indexOf("@")).trim(); //Get rid of the message
    
let email_array = email_string.split(" "); //Split by " " to get the different emails
    
console.log(email_array);

答案 2 :(得分:0)

您可以通过搜索@后跟非空格来匹配电子邮件。

var string = 'new message! @user1@example.com @user2@example.com',
    emails = string.match(/@\S+/g);

console.log(emails);