SoFunction
Updated on 2025-03-02

Regular expression matching URL (recommended)

Regular expressions describe a pattern of string matching, which can be used to check whether a string contains a certain substring, replace the matching substring, or take out a substring that meets a certain condition from a string, etc.

Then expression:

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

match:

(1) Directly match the domain name address:

var matchString = '/';
((matchString)); // ==> true

(2) The address of the matching link with the suffix (*.htm,*.html,*.php,*.aspx...) is:

var matchString = '/';
((matchString)); // ==> true

(3) Match the address containing parameters:

var matchString = '/?opt=1';
((matchString)); // ==> true

Instructions for use:

(1) The address must start with http/https/ftp/ftps;

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

The above is the regular expression for matching URLs introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!