RegexBuilder · regex answers
Regex to find a repeated character
/(.)\1+/g
Capture any character, then require at least one more of the same via the backreference. Every run of two or more identical characters becomes a match, which is how you spot 'aaa' or a doubled separator without listing candidates.
What each part does
| (.) | capture one character |
| \1+ | one or more repetitions of that exact character |
Tested against real input
Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.
| Input | Result | Matched |
|---|---|---|
| aaa | matches | aaa |
| hello | matches | ll |
| a--b | matches | -- |
| abc | no match | — |
| abab | no match | — |
The mistake to avoid
Writing (.)\1{2,} when you meant three or more in total. The group already matched one, so {2,} means three or more overall — off-by-one lives here.
Try it on your own text
Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.
Open RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID