1. 程式人生 > >js_判斷當前url是否合法http(s)

js_判斷當前url是否合法http(s)

sed gif false exp 判斷 har urn ssi function

 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>
View Code

js_判斷當前url是否合法http(s)