我想使用 Javascript 解析一条推文并返回一个包含该推文中提到的人的数组。 Twitter 用户名都以@开头。假设我已经有了字符串,这怎么办?
最佳答案
var tweet = "hello to @you and @him!";
var users = tweet.match(/@\w+/g);
console.log(users); // Will return an array containing ["@you", "@him"]
然后,您可以删除 @
以仅获取名称:
for (userIndex = 0; userIndex < users.length; userIndex++)
users[userIndex] = users[userIndex].substr(1);
然后将数组返回为
["you", "him"]
关于javascript - 如何使用 Javascript 从推文中提取 Twitter 用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665279/