如何使用Javascript从推文中提取Twitter用户名

nod*_*nja 1 javascript string twitter parsing

我想使用Javascript来解析推文并返回一个包含该推文中提到的人的数组.Twitter用户名都以@开头.假设我已经有了字符串,怎么办呢?

F.P*_*F.P 5

var tweet = "hello to @you and @him!";
var users = tweet.match(/@\w+/g);
console.log(users); // Will return an array containing ["@you", "@him"]
Run Code Online (Sandbox Code Playgroud)

然后,您可以剥离@只获取名称:

for (userIndex = 0; userIndex < users.length; userIndex++)
    users[userIndex] = users[userIndex].substr(1);
Run Code Online (Sandbox Code Playgroud)

然后将返回数组作为

["you", "him"]
Run Code Online (Sandbox Code Playgroud)