Javascript Data Type How to - Extract Twitter usernames from a tweet








Question

We would like to know how to extract Twitter usernames from a tweet.

Answer


<!DOCTYPE html>
<html>
<body>
<script type='text/javascript'>
<!--   w  w w  .  j a  va  2s  .co  m-->
var tweet = 'This tweet is for @me and @you #hashtag';
var matches = tweet.match(/@\w+/g);
for(var i=0; i<matches.length; i++) {
    document.writeln(matches[i]);
    document.writeln('<br/>');
}

</script>
</body>
</html>

The code above is rendered as follows: