Incautiously crafted regexes can be a major performance bottleneck (the upcoming section, Runaway Backtracking, contains several examples showing how severe this can be), but there is a lot you can do to improve regex efficiency. Just because two regexes match the same text doesn’t mean they do so at the same speed.
Many factors affect a regex’s efficiency. For starters, the text a regex is applied to makes a big difference because regexes spend more time on partial matches than obvious nonmatches. Each browser’s regex engine also has different internal optimizations.
Regex optimization is a fairly broad and nuanced topic. There’s only so much that can be covered in this section, but what’s included should put you well on your way to understanding the kinds of issues that affect regex performance and mastering the art of crafting efficient regexes.
In order to use regular expressions efficiently, it’s important to understand how they work their ...