js regular function of checking whether the input is a URL is also very common in web pages. When filling in the friendly link section and form on the personal homepage, JavaScript is used to verify whether it is a URL.
This test is difficult to write. It is best to use regular expressions for authentication.
stipulates that the input can only start with http:// and https://, and it must be a URL.
Some people say, why can’t web pages like www.1.com work?
This is to prevent you from using user input to construct a hyperlink. If the href attribute in the a tag does not encounter http:// or https://, then it will be considered It is the root directory. This address will be written after the URL of your website and then jump. Everyone should know this. For example,xxx, my URL is http://localhost, then after clicking the a tag that displays xxx, it will just jump to http:// The location localhost/www.1.com is of course wrong.
For example, in the text box below, how can we use regular expressions to require the user to enter a URL starting with http:// and https://?
1. The first is a simple layout, which goes without saying:
无标题文档 网址必须以http://或者https://开头,且必须是个网址^_^!
2. The second is the script, which goes without saying. The key is That regular expression:
in: var reg=/^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][ sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~/])+$/;among them,
1. In Javascript, since all variables are var, the regular expression must be written between two slashes, /.../, and then the slash / in the regular expression must be written as /
2. ^ means It must start with..., [] represents a test unit, which is what a certain character can hold, such as ^([hH][tT]{2}[pP]://|[hH][tT]{2 }[pP][sS]://), which means it is required to start with http:// or https://. | is or, the first character is h or H, the second and third characters are [tT], {2} means that this character and the following character must be [tT], and so on.
3. ([A-Za-z0-9-~]+) means that this character and its subsequent characters must be uppercase letters, lowercase letters, numbers, minus signs - or ~
characters + It means: match the character before the + sign 1 time or n times, for example: /a+/ matches the 'a' in "candy" and all the 'a' in "caaaaaaandy".
4. Therefore (([ A-Za-z0-9-~]+).)+ means XXX. This thing ending with a dot must appear at least 1 before the character ([A-Za-z0-9-~/])+$ Times
5. $ means it must end with uppercase letters, lowercase letters, numbers, minus signs -, ~, /
The above is the entire content of this article, I hope it will be helpful to everyone's study , I also hope that everyone will support the PHP Chinese website.
For more js using regular expressions to check whether the input content is a URL, please pay attention to the PHP Chinese website!