1
我想用Javascript解析推文并返回包含该推文中提到的人的数组。 Twitter用户名全部以@开头。假设我已经有了字符串,那怎么做呢?如何使用Javascript从推文中提取Twitter用户名
我想用Javascript解析推文并返回包含该推文中提到的人的数组。 Twitter用户名全部以@开头。假设我已经有了字符串,那怎么做呢?如何使用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"]
var tweet = 'This tweet is for @me and @you #hashtag';
var matches = tweet.match(/@\w+/g);
任何全源代码示例html-javascript? – Kiquenet 2013-10-18 14:33:30