Username Regex Archives - Thoughts https://blog.jstassen.com/tag/username-regex/ A place for collecting my miscellaneous thoughts. Tue, 07 Dec 2021 07:01:10 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.7 92836340 Code: Regex for Instagram Username and Hashtags https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/ https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/#comments Mon, 14 Mar 2016 21:31:29 +0000 http://blog.jstassen.com/?p=716 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 … Continue reading Code: Regex for Instagram Username and Hashtags →

The post Code: Regex for Instagram Username and Hashtags appeared first on Thoughts.

]]>
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

(?:@)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)
Instagram username RegEx

Hashtags

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

(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)

Examples

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

The post Code: Regex for Instagram Username and Hashtags appeared first on Thoughts.

]]>
https://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/feed/ 6 716