Social
Instagram username Regex
Instagram usernames may contain letters, digits, underscores and dots, up to 30 characters in total. Dots cannot appear at the start or end, and two dots cannot sit next to each other. This pattern enforces all of those rules: a lookahead caps the length, and the body only allows a dot when a regular character follows it. It is helpful for influencer databases, profile link fields and form checks. Instagram also blocks some reserved words and names that are already taken, which a regex cannot know about, so treat a match as a well-formed name rather than an available one.
Pattern (JavaScript)
/^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$/Starts at the start of the line/string, then a position followed by (any character, between 1 and 30 times, then the end of the line/string), then one of the characters A-Za-z0-9_, then (a literal ".", optionally (zero or one time), then one of the characters A-Za-z0-9_), zero or more times, then the end of the line/string.
Examples
Matches
- ✓ test_user
- ✓ test.user_01
- ✓ a
- ✓ sample.account.2026
Does not match
- ✗ test..user
- ✗ .testuser
- ✗ testuser.
- ✗ test user
- ✗ abcdefghijabcdefghijabcdefghij1
Try it
0 matches (JavaScript engine)
test_user
test.user_01
a
sample.account.2026
test..user
.testuser
testuser.
test user
abcdefghijabcdefghijabcdefghij1Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$/g;
const text = "test_user";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$')
text = 'test_user'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$/';
$text = 'test_user';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?=.{1,30}$)[A-Za-z0-9_](?:\\.?[A-Za-z0-9_])*$");
String text = "test_user";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ Go's RE2 engine has no lookahead, so the 30 character cap cannot be enforced exactly; strings with dots may exceed 30 characters. Check len(name) <= 30 in code.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z0-9_](?:\.?[A-Za-z0-9_]){0,29}$`)
text := "test_user"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$");
var text = "test_user";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
ℹ The regex crate has no lookahead, so the 30 character cap cannot be enforced exactly; strings with dots may exceed 30 characters. Check name.len() <= 30 in code.
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Za-z0-9_](?:\.?[A-Za-z0-9_]){0,29}$").unwrap();
let text = "test_user";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?=.{1,30}$)[A-Za-z0-9_](?:\.?[A-Za-z0-9_])*$/
text = 'test_user'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why use a lookahead for the length?
- The body of the pattern consumes one or two characters per repetition, so counting repetitions does not give the exact length. The (?=.{1,30}$) lookahead checks the total length once, before the body runs.
- Can an Instagram username end with an underscore?
- Yes. Underscores are allowed anywhere, including at the start and end. Only dots have position rules, which is why the pattern treats them separately.
- Does a match mean the username is available?
- No. The pattern only checks the format. Availability, reserved words and banned names are decided by Instagram and can only be confirmed through the platform itself.