Regex匹配URL

來源:互聯網
上載者:User

標籤:blog   pre   地址   正則表達   ftp   code   log   地方   str   

Regex:

var match = /^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/;
/* 
註:
(1)、如需允許其他聯結方式,可以修改“(ht|f)tps?”部分,在“?”後面跟上符號“|”,然後加上您需要的聯結方式,多個時用符號“|”分隔)。
(2)、如需允許URL參數包含其它字元,可以修改“[\w\-\.,@?^=%&:\/~\+#]”,以設定您需要的參數。
*/

匹配:

(1)、直接匹配網域名稱地址:

var matchString = ‘https://i.cnblogs.com‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com/‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com//‘; // ==> 不允許非網域名稱或參數以外的地方出現雙“/”;console.log(match.test(matchString)); // ==> false

(2)、匹配連結含(*.htm,*.html,*.php,*.aspx...)尾碼的地址:

var matchString = ‘https://i.cnblogs.com/EditPosts.aspx‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com./EditPosts.aspx‘; // ==> 不允許參數以外的地方以雙“.”結尾;console.log(match.test(matchString)); // ==> false

(3)、匹配含參數的地址:

var matchString = ‘https://i.cnblogs.com/EditPosts.aspx?opt=1‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com/EditPosts.aspx?opt=1&user=‘console.log(match.test(matchString)); // ==> true

使用說明:

(1)、地址必須以http/https/ftp/ftps開頭;

(2)、地址不能包含雙位元組符號或非連結特殊字元。

 

Regex匹配URL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.