Regular expression matching URL

Source: Internet
Author: User

Regular Expressions:

var match =/^ ((ht|f) tps?): \ /\/([\w\-]+ (\.[ \w\-]+) *\/) *[\w\-]+ (\.[ \w\-]+) *\/? (\? ([\w\-\.,@?^=%&:\/~\+#]*) +)?/;

Note:
(1), if you need to allow other connection methods, you can modify the "(Ht|f) TPS?" section, in the "?" Follow the symbol "|" followed by the Join method you need , with the symbol "|" Separated).
(2), if you want to allow the URL parameter contains other characters, you can modify "[\w\-\.,@?^=%&:\/~\+#]" To set the parameters you need.
*/

The:

(1), directly match the domain name address:

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//'; ==> does not allow double "/" in places other than domain names or parameters; Console.log (Match.test (matchstring)); ==> false

(2), matching links included (*.htm,*.html,*.php,*.aspx ...) Address of suffix:

var matchstring = ' https://i.cnblogs.com/EditPosts.aspx '; Console.log (Match.test (matchstring)); ==> true
var matchstring = ' https://i.cnblogs.com./EditPosts.aspx '; ==> does not allow a double "." In places other than parameters. End; Console.log (Match.test (matchstring)); ==> false

(3), matching the address with parameters:

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

Instructions for use:

(1), address must start with Http/https/ftp/ftps;

(2), the address cannot contain double-byte symbols or non-linked special characters.

Regular expression matching URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.