且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-19 09:29:58

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

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

Then, you can strip the @ to only get the names:

for (userIndex = 0; userIndex < users.length; userIndex++)
    users[userIndex] = users[userIndex].substr(1);

然后将其返回为

["you", "him"]