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?!
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With