Menu

Regex not matching? A debugging checklist
📰

Regex not matching? A debugging checklist

DEV Community: javascript·Ijaz Ur Rahim·6 days ago
#aPlwWJc1
#devtojavascript#devto#dev#pattern#regex
Long article (1188 words) — no AI summary yet.
Reading 0:00
15s threshold

When a regex looks right but doesn't match, the pattern is rarely wrong in an interesting way. It's usually one of a dozen ordinary causes: the input has a character you can't see, a backslash was lost in a string, a special character isn't escaped, an anchor or flag is missing, or the code runs the regex differently from your tester . Below is a checklist in the order worth checking, with examples.

The examples run JavaScript's engine; where Python or another language behaves differently, the item says so.

First, a 60-second method

Print the input exactly. In Python, print(repr(text)) shows hidden characters as escapes like \xa0 (a non-breaking space), \u200b (a zero-width space) or \r . In JavaScript, [...text].map(c => c.codePointAt(0).toString(16)) lists every character's code, so a0 or 200b stands out.

Shrink the pattern. Delete pieces from the end until it matches, then add them back one by one.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.