DD
DevDash

Regex for Social Handle (@)

Regex Pattern

^@[a-zA-Z0-9_]{1,30}$

Social media handle with @ prefix (1-30 chars)

Quick Answer

The regex pattern for social handle (@) is `^@[a-zA-Z0-9_]{1,30}$`. Social media handle with @ prefix (1-30 chars). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
@username✓ Matches
@John_Doe✓ Matches
@user123✓ Matches
username✗ No match
@✗ No match
@user-name✗ No match
@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a@a✗ No match

Code Examples

javascript

const regex = /^@[a-zA-Z0-9_]{1,30}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^@[a-zA-Z0-9_]{1,30}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^@[a-zA-Z0-9_]{1,30}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^@[a-zA-Z0-9_]{1,30}$/', $value)) {
    echo "valid";
}

java

String pattern = "^@[a-zA-Z0-9_]{1,30}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.