Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this regex make Chrome hang?

Try typing this into Chrome's JS console. It's a regex I found to check if something's a valid URL or not:

"http://www.kvraudio.com/".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);

A match is returned, as it should be. Now try this:

"tp:/www.kvraudio.com/forum/viewtopic.php".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);

Null is returned, because it doesn't match. Now..... try this:

"http://www.kvraudio.com/forum/viewtopic.php?p=5238905".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);

Nothing! JS appears to be dead or stuck in a loop somehow. If I use the above in an actual web page, it stops responding. Won't even scroll! Anyone got any sort of explanation for that then? What have I done wrong?!

like image 357
BaronVonKaneHoffen Avatar asked Jun 14 '13 20:06

BaronVonKaneHoffen


1 Answers

Because you have catastrophic backtracking:

([\/\w \.-]*)*

This expression should be modified to remove one of the stars (*):

([\/\w \.-]*)

Note that catastrophic backtracking typically only rears its ugly head when a match cannot be made. That's why the first example you gave executes without any issues.

Your second example exits before it hits the ([...]*)*, so there is no opportunity for the backtracking to take effect.

For a more thorough explanation of catastrophic backtracking, see my answer to this question:
How can I recognize an evil regex?

like image 138
JDB Avatar answered Dec 04 '22 09:12

JDB