Code: Regex for Instagram Username and Hashtags

Instagram is unique when it comes to the rules for usernames and hashtags. Here is a regex to capture and validating them with the JavaScript regex engine.

The rules

  • Two matches @ mentions with no space between @thebox193@discodude
  • Matches with one . in them @disco.dude but not two .. @disco..dude
  • Beginning period not matched @.discodude
  • Ending period not matched @discodude.
  • Match underscores _ @_disco__dude_
  • Max characters of 30 @1234567890123456789012345678901234567890

The RegEx

Instagram username RegEx


For hashtags use the same regex, it’s the same rules, just replace the @ for a #.



You can try it out over on Debuggex. Here is a an example implemented in JavaScript.

  • Akbar Amani

    Thank you so much, This is the best regular expression for Instagram username

  • Geo

    @JStassen, thank you for sharing this. How would you modify if you were to do it without the ‘@’? Infact, if we had to check to make sure that no one had entered the @ to say storing it in the database?

    • TheBox193

      @disqus_gm92v83kAy:disqus You should be able to just take off the first section of the regex (?:^|[^w])(?:@).

      If you’re parsing text you’re going to need to loop over your text looking for all matches, added them to an array.

      Here is an example:,console

  • Pingback: Regular Expressions | New Life()

  • Carlos Pinto

    Hi thanks for providing this. It works well for usernames, but the title is misleading since it doesn’t cover hashtags. You need the or operator in the first segment of the regex: (?:@|#) so that it includes the @ and the #.

    • Ah you’re right! It’s the same rules I believe, I’ve just use the same regex but swap the @ for the #. Or you can combine them like you say. Nice catch!