Regex Valid Twitter Mention

I have found that this is the best way to find mentions inside of a string in javascript. I don't know exactly how i would do the RT's but I think this might help with part of the problem.

var str = "@jpotts18 what is up man? Are you hanging out with @kyle_clegg";
var pattern = /@[A-Za-z0-9_-]*/g;
str.match(pattern);
["@jpotts18", "@kyle_clegg"]

I guess something like this will do it:

^(?!.*?RT\s).+\s@\w+

Roughly translated to:

At the beginning of string, look ahead to see that RT\s is not present, then find one or more of characters followed by a @ and at least one letter, digit or underscore.


This regexp might work a bit better: /\B\@([\w\-]+)/gim

Here's a jsFiddle example of it in action: http://jsfiddle.net/2TQsx/96/


Here's a regex that should work:

/^(?!.*\bRT\b)(?:.+\s)?@\w+/i

Explanation:

/^             //start of the string
(?!.*\bRT\b)   //Verify that rt is not in the string.
(?:.*\s)?      //Find optional chars and whitespace the
                  //Note: (?: ) makes the group non-capturing.
@\w+           //Find @ followed by one or more word chars.
/i             //Make it case insensitive.