Regex for Matching Repeated Characters
Uses a backreference to match any character that immediately repeats itself.
Published October 13, 2026
This regex uses a backreference to find a character repeated consecutively, like 'aa' or '11'.
How the pattern works
Breaking the pattern down: (.) = capture any character; \1 = same character again.
Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.
Pattern tests
/(.)\1/
✓ 'hello'
✓ 'aa'
✓ '1223'
✗ 'abc'
✗ '1234'
Practical examples
The pattern
const re = /(.)\1/Test it
const re = /(.)\1/
re.test('hello') // true
re.test('aa') // true
re.test('abc') // false
re.test('1234') // falseIn Python
import re
re.search(r'(.)\1', text)Common mistakes
- Matching newlines needs the s flag.
- Longer runs (3+) need \1{2,}.
- Case sensitivity.
Key takeaways
- Anchors (^ and $) make the whole string match instead of a part of it.
- Regex checks format only; verify data separately when it matters.
- Test with valid and invalid samples.
FAQ
Three in a row?
Use (.)\1{2}.
Where can I test this pattern?
Use the Regex Tester on this site, or regex101.com.