I am currently using this code to validate URL’s but since i am validating highlighted text from a page it’s really really slow for a javascript function.
Here is the code i am using:
var urlregex = new RegExp(
"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test($.trim(url));
I hope that there is other methods that i can use to check for valid links using javascript.
Thanks.
It is considerably slower to use
expr = new RegExp("pattern")vs.expr = /pattern/g. You should also make sure that you compile the expression outside of your function definition so it can be reused without being compiled every time the function is called (even more important if it is inside a loop). Try the following:You can see more about javascript regular expression performance here: http://corporate.tuenti.com/en/dev/blog/tips-and-tricks-with-regular-expressions-in-js