Regex to Match Common Palindrome Words
Match five-letter palindrome words where the characters read the same forward and backward using regex backreferences. Free online regex pattern tester.
Regular Expression
/\b([a-zA-Z])([a-zA-Z])[a-zA-Z]\2\1\b/gi
Token Breakdown
| Token | Description |
|---|---|
| \b | Word boundary assertion |
| ( | Start of capturing group |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| ) | End of group |
| ( | Start of capturing group |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| ) | End of group |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| \2 | Escaped character '2' |
| \1 | Escaped character '1' |
| \b | Word boundary assertion |
Detailed Explanation
This regex matches five-letter palindrome words using backreferences to enforce mirror symmetry. Here is the token-by-token breakdown:
\b — A word boundary assertion ensuring the match starts at the beginning of a word. This prevents matching palindromic substrings within larger non-palindromic words.
([a-zA-Z]) — Capturing group 1 matches the first letter of the word. This letter must also appear as the last letter of the palindrome.
([a-zA-Z]) — Capturing group 2 matches the second letter. This letter must also appear as the fourth letter of the palindrome.
[a-zA-Z] — Matches the third (middle) letter of the five-letter word. This is the pivot of the palindrome and does not need to be repeated, so it is not captured.
\2 — Backreference to capturing group 2, matching the exact same character as the second letter. This creates the fourth letter, which mirrors the second.
\1 — Backreference to capturing group 1, matching the exact same character as the first letter. This creates the fifth letter, which mirrors the first.
\b — A word boundary assertion ensuring the match ends at the end of a word. This prevents matching palindromic substrings within larger words.
The g flag enables global matching and the i flag makes the match case-insensitive. This pattern demonstrates the power of regex backreferences for enforcing character symmetry.
Common five-letter palindrome words this pattern matches include: level (l-e-v-e-l), refer (r-e-f-e-r), kayak (k-a-y-a-k), madam (m-a-d-a-m), and civic (c-i-v-i-c). The pattern is limited to exactly five-letter palindromes due to the fixed structure. For palindromes of other lengths, separate patterns with different numbers of capturing groups would be needed.
Example Test Strings
| Input | Expected |
|---|---|
| level | Match |
| refer | Match |
| hello | No Match |
| kayak | Match |
| world | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
36 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex