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?
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.
here is how you extract mentions from instagram caption with JavaScript and underscore.
I have found that this is the best way to find mentions inside of a string in javascript.
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.
Please let me know if you have used anything that is more efficient, or accurate. Thanks!