Javascript URL验证正则表达式 [英] Javascript URL validation regex

查看:82
本文介绍了Javascript URL验证正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址验证正则表达式:

I had the following url validation regex:

/(ftp | https?):// [^] + $ /

/(ftp|https?)://[^ "]+$/

这是来自ref:
URL验证的正则表达式(在JavaScript中)

这样可以正常工作,包括 http:// localhost: 8080 ,但它也验证了我不想要的下面的那些。上面的正则表达式不应该通过以下网址

This works fine including cases like http://localhost:8080, but it also validates the below ones that i dont want. The above regex should not pass the below urls

 1. http://www.example..com
 2. http://.com
 3. http://.
 4. http://www.example. 

请帮助我作为正则表达式的菜鸟

Kindly help as am a noob in regex

第二个问题,虽然与问题没有关系,但正则表达式是,当我验证null和未定义的正则表达式 / ^ [az] + $ / i 时,我得到了。这是默认行为或我错过了什么?

Second question, though not related to the question but regex is, when i validate null and undefined against the regex /^[a-z]+$/i i get true. Is this the default behavior or am i missing something over here?

推荐答案

试试他的

function is_valid_url(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

调用 is_valid_url(YOUR_WEBSITE_URL)在网站验证的任何地方运行。

Call the is_valid_url(YOUR_WEBSITE_URL) function everywhere which you want to website validate.

这篇关于Javascript URL验证正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆