Javascript uses regular expressions to check whether the input content is a URL or a js regular expression.
Js regular expressions are also common in Web pages. When you enter the personal homepage in the links and forms, use JavaScript to verify whether the input is a Web site.
This test is hard to write. It is best to use a regular expression for authentication.
It is specified that the entered content must start with http: // and https: //, and must start with a web site.
Some people say, why does a webpage like www.1.com fail?
This is to prevent the href attribute in the tag from failing to meet http: // or https: // when you construct a hyperlink using the user input, it will be considered as the root directory, and will be written to this address after the URL of your website. This should be known to everyone. For example, if <a href = "www.1.com"> xxx </a> and my website is http: // localhost, click the tag displayed as xxx, it just jumps to the http: // localhost/www.1.com location. Of course not.
For example, in the text box below, how can we use a regular expression to require users to enter URLs starting with http: // and https?
1. The first is a simple layout. Needless to say:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. The second is the script, which is not required. The key is the regular expression:
<Script> function CheckUrl () {var url = document. getElementById ("url "). value; var reg =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) ([A-Za-z0-9-~] +) \.) + ([A-Za-z0-9 -~ \/]) + $/; If (! Reg. test (url) {alert ("this url does not start with http: // https: // or is not a url! ") ;}Else {alert (" input successful ") ;}</script>
In:Var reg =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \ //) ([A-Za-z0-9-~] +) \.) + ([A-Za-z0-9 -~ \/]) + $ /;,
1. In Javascript, because all variables are var, the regular expression must be written in two slashes,/.../, and then in the Regular ExpressionSlash/must be written \/
2. ^ indicates that ...... [] Indicates a unit of test, that is, something that a character can hold, such as ^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/), that is, the request must be http: // or https: // start. | Yes or, the first character is h or H, and the second and third characters are [tT], {2} indicates that the character and the first character after it must both be [tT], and so on.
3,([A-Za-z0-9-~] +)It must contain uppercase letters, lowercase letters, numbers, minus signs (-), or ~
The character + indicates that the character before the plus sign is matched once or n times. For example: /a +/match all 'A' in "candy" and "caaaaaaandy '.
4. Therefore([A-Za-z0-9-~] +) \.) +It means XXX. This thing ending with a point must be in ([A-Za-z0-9 -~ \/]) + $ This character must appear at least once
5. $ indicates that uppercase letters, lowercase letters, numbers, minus signs (-), and ~ are required -,~ ,/End
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.