Regex
regex
December 18, 20223 min read

Regex for @mentions match

In social media, a mention is when a user includes another user's username in their post, tweet, or other content. This is a way for users to communicate with one another or to bring attention to another user's content. When someone mentions another user in a post, that user's username is typically preceded by the at symbol (@).

For example, if a user named "JohnDoe" wanted to mention another user named "JaneDoe" in a tweet, they might write: "Hey @JaneDoe, great job on that presentation!" When a user is mentioned in this way, they will typically receive a notification alerting them to the mention, and the mention will also be visible to other users who view the post. Mentioning other users is a common way for users to interact with one another on social media platforms and can be used for a variety of purposes, such as asking questions, making requests, or simply acknowledging someone's contribution. In this article let's understand how we can create a regex for extracting mentions from a string and how regex can be matched for mentions.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of @Mentions

  • It should start with a @
  • It can be followed by any number of characters
  • It can also be accompanied by text/string
  • It can exist independently as #string

Regex for matching and extracting @mentions from a string

Regular Expression-

Containing minimum 8 characters, with at least 1 letter and 1 number-

/@\w+/gm

Test string examples for the above regex-

Input StringMatch Output
get work donedoes not match
@brotha wassup?matches
we rockdoes not match
how are you @orton?matches

Here is a detailed explanation of the above regex-

/#\w+/gm

@ matches the character @ with index 6410 (4016 or 1008) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)

Hope this article was useful to matching and extracting mentions from string using regex pattern.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 1, 20223 min read
Next Article
December 18, 20224 min read