1 alert(checkURL('http:555')); //false 2 function checkURL(URL) { 3 var str = URL, 4 Expression = /http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?/, 5 objExp = new RegExp(Expression); 6 if(objExp.test(str) == true) { 7 return true 8 } else { 9 return false 10 } 11 }
全部代码:
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 </head> 8 9 <body> 10 <script type="text/javascript"> 11 alert(checkURL('http:555')); //false 12 function checkURL(URL) { 13 var str = URL, 14 Expression = /http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?/, 15 objExp = new RegExp(Expression); 16 if(objExp.test(str) == true) { 17 return true 18 } else { 19 return false 20 } 21 } 22 </script> 23 </body> 24 25 </html>