Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pull @ mentions out of strings like twitter in javascript

I am writing an application in Node.js that allows users to mention each other in messages like on twitter. I want to be able to find the user and send them a notification. In order to do this I need to pull @usernames to find mentions from a string in node.js?

Any advice, regex, problems?

like image 266
jpotts18 Avatar asked Mar 07 '13 07:03

jpotts18


2 Answers

I have found that this is the best way to find mentions inside of a string in javascript.

var str = "@jpotts18 what is up man? Are you hanging out with @kyle_clegg";
var pattern = /\B@[a-z0-9_-]+/gi;
str.match(pattern);
["@jpotts18", "@kyle_clegg"]

I have purposefully restricted it to upper and lowercase alpha numeric and (-,_) symbols in order to avoid periods that could be confused for usernames like (@j.potts).

This is what twitter-text.js is doing behind the scenes.

// Mention related regex collection
twttr.txt.regexen.validMentionPrecedingChars = /(?:^|[^a-zA-Z0-9_!#$%&*@@]|RT:?)/;
twttr.txt.regexen.atSigns = /[@@]/;
twttr.txt.regexen.validMentionOrList = regexSupplant(
    '(#{validMentionPrecedingChars})' +  // $1: Preceding character
    '(#{atSigns})' +                     // $2: At mark
    '([a-zA-Z0-9_]{1,20})' +             // $3: Screen name
    '(\/[a-zA-Z][a-zA-Z0-9_\-]{0,24})?'  // $4: List (optional)
  , 'g');
twttr.txt.regexen.endMentionMatch = regexSupplant(/^(?:#{atSigns}|[#{latinAccentChars}]|:\/\/)/);

Please let me know if you have used anything that is more efficient, or accurate. Thanks!

like image 80
jpotts18 Avatar answered Sep 23 '22 06:09

jpotts18


Twitter has a library that you should be able to use for this. https://github.com/twitter/twitter-text-js.

I haven't used it, but if you trust its description, "the library provides autolinking and extraction for URLs, usernames, lists, and hashtags.". You should be able to use it in Node with npm install twitter-text.

While I understand that you're not looking for Twitter usernames, the same logic still applies and you should be able to use it fine (it does not validate that extracted usernames are valid twitter usernames). If not, forking it for your own purposes may be a very good place to start.

Edit: I looked at the docs closer, and there is a perfect example of what you need right here.

var usernames = twttr.txt.extractMentions("Mentioning @twitter and @jack")
// usernames == ["twitter", "jack"]
like image 38
Nick Mitchinson Avatar answered Sep 20 '22 06:09

Nick Mitchinson