Poorly performing regex

Posted by Kieron on Stack Overflow See other posts from Stack Overflow or by Kieron
Published on 2010-04-22T14:12:47Z Indexed on 2010/04/22 14:23 UTC
Read the original article Hit count: 574

Filed under:
|
|

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,

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript