Home > Software design >  @username Regular Expression for social media with JavaScript
@username Regular Expression for social media with JavaScript

Time:11-26

I am relatively new to using Regular Expressions with JavaScript and come across a particular situation with regards to the '@' username pattern that has given me about 3 hours of trouble.

I would like to replicate the @username pattern found on social media text fields on websites such as Facebook and Twitter. (I understand that they parse tokenized macros but I would like a pure RegEx version).

I have attached an image of the closest pattern that I have achieved, however I will also type this in order to make it easier for anyone to copy and paste into their own RegEx pattern checker.

As you can see, the @ symbol aught to catch all subsequent alpha characters and nothing preceding that @ symbol and be terminated by a space. There is a special case where a URL that contains an @ symbol should be ignored. (As illustrated in the image) and the @ symbol could be used at the very start of the textfield and in this case the handle should be parsed.

Clearly, my existing pattern is collecting the preceding character to the @ symbol which is incorrect. Any help would be fantastic.

RegEx101 example (with highlights)

RegEx101 example

RegEx that is not working

/(^^|[^\/])(@[A-Za-z0-9_.]{3,25})/gm

Text version for copy paste convenience

@test testing
 @test testing
testing ,@test
https://youtube.com/@test

I tried multiple combinations of patterns, over 3 hours, to try to isolate @handle style tags as seen in popular social networks. I expected to be able to isolate only the portion of the patter that contain a single @ deliminated username. I expected that I could ignore this patter where it is part of a URL.

My actual results cause the preceding character to be collected and added to the final string which is incorrect.

CodePudding user response:

It sounds like you want a positive lookbehind

(?<=YOUR_REGEX)
/(?<=^|[^\/])(@[A-Za-z0-9_.]{3,25})/gm

enter image description here

CodePudding user response:

I see you've already accepted an answer, but here's another version that may work for what you're trying to do.

/(?<=[^\/]|^)@[A-Za-z0-9_.]{3,25}/g

This matches anything that...

  • Is at the start of the line OR is not preceeded by a forwards slash (/)
    • Credit to @Samathingamajig for pointing that out.
  • Starts with an @ symbol
  • Is followed by between 3 and 25 alphanumeric (incl. _ and .) characters.

Screenshot of the above regular expression being checked against the provided test cases.

  • Related