三款Regex驗證網址

來源:互聯網
上載者:User

(http://)?代表匹配http://0或1次,
([w.]+)匹配www.111cn.net
/[w- ./?%&=]*)?匹配後面子串0或1次

^(http(s)?://)?(www.)?[w-]+.w{2,4}(/)?$

 

<script type="text/網頁特效 ">
function isurl(str){
var regurl = /(http://)?([w.]+)(/[w- ./?%&=]*)?/gi;
var result = str.match(regurl);
if(result!=null) {alert( "yes ")}
else{alert( "no! ")}
}
</script>

方法二

//在javascript中,Regex只能使用"/"開頭和結束,不能使用雙引號
//判斷url地址的Regex為:http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
//下面的代碼中應用了逸出字元""輸出一個字元"/"

<script language="javascript">
function checkuserinfo(){
var homepage=userinfo.homepage.value;
if(homepage==""){
alert("請輸入個人首頁地址!");
document.userinfo.homepage.focus();
}else{
if(!checkeurl(homepage)){
alert("您輸入的個人首頁地址不合法!");
document.userinfo.homepage.focus();
return;
}
}
userinfo.submit();
}
function checkeurl(url){
var str=url;

var expression=/http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?/;
var objexp=new regexp(expression);
if(objexp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.