I’ve a really poorly performing piece of regex, currently it makes Firefox, Chrome and IE hang for a period of time.
Here’s the reg-ex:
^([a-zA-Z0-9]+[/]?)+[a-zA-Z0-9]+$
It’s kind of a url matcher, but should only match the requested path (not starting with or ending with a slash).
Valid examples:
- Segment
- Segment/Segment
- segment/segment/Segment (etc)
Invalid examples:
- /Segment
- Segment/
- Segment/Segment/
Using the regex above over all three browsers and using two or more slashes causes the browsers to hang.
It’s obviously a poorly formed reg-ex, but can anyone help build a better one?
Thanks,
A better one would be more deterministic, and without capturing groups:
This way you don’t have overlapping groups, and you’re not capturing things unnecessarily.