DEV Community

Discussion on: What are your code smells and/or best practices with regular expressions?

Collapse
 
lexlohr profile image
Alex Lohr

When I finally really understood regular expressions, I was overusing it for everything for a short time. My colleagues were joking that my code was becoming unreadable for being riddled with/by RegExp and that I was writing more "strange Emojis" than JavaScript.

My current best practice is: if the comment that is necessary to explain the RegExp to my colleagues is longer than the RegExp itself, it's better to split it into multiple parts or rewrite the code into an understandable parser. Vice versa, the code smell would be any RegExp that requires a comment longer than itself to understand it will in 99% of all cases better be rewritten without (too long) regular expressions.

The other (obvious) code smell is the attempt to use regular expressions to solve irregular problems (like HTML).