正则表达式匹配(URL、电话、手机、邮箱)的实例代码
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。下面通过实例代码给大家介绍正则表达式匹配(URL、电话、手机、邮箱)的实例代码,一起看看吧!
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则验证</title> </head> <body> <input type="text" name="" id="text"> <input type="button" name="" id="btn" value="点击"> <span></span> </body> <script type="text/javascript"> var text = document.getElementById('text'); var btn = document.getElementById('btn'); //1、url示例: //https://www.baidu.com/s?wd=%E5%88%98%E5%BE%B7%E5%8D%8E&rsv_spt=1&rsv_iqid=0x9601edc200017402&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&oq=javascript%2520%25E9%259A%25BE%25E9%25A2%2598&inputT=1256&rsv_t=7da5GmMhTie86h8qaOiaV047P9TkJunMjrkmK%2BNLA%2FuJs3bMG%2Bj52w%2F6IxHge5MB%2B%2B%2Fw&rsv_pq=c270d50b00019cae&rsv_sug3=21&rsv_sug1=19&rsv_sug7=100&rsv_sug2=0&rsv_sug4=1818 //2、电话示例: //13800138000 //+ 8610 - 59926666 //+ (1)533- 222 - 334 //010 - 59926666 //037132376865 //3、电子邮件示例: //[email protected] //[email protected] //[email protected]_domain_name.com.uk // var regexp=/^([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+$/;//邮箱 // var regexp=/(https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.))/;//URL // var regexp=/\+?\d{3,4}-?\d{7,8}/; // 010-59926666 8610-59926666 037132376865 // var regexp =/\+\(\d\)(\d{3}-){2}\d{3}/;//+(1)533-222-334 // var regexp=/(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/;//电话号码 13800138000 010-59926666 8610-59926666 037132376865 +(1)533-222-334 var regexp=/(([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+)|((https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.)))|(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/; btn.onclick = function(){ if(regexp.test(text.value)){ alert('验证通过'); text.value=""; }else{ alert('验证没通过'); } } </script> </html>
总结
相关推荐
AHuqihua 2020-06-14
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09