Home  >  Article  >  Web Front-end  >  Regular expression method to match URL, phone number, mobile phone, and email address

Regular expression method to match URL, phone number, mobile phone, and email address

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 11:29:242144browse

This time I will bring you Regular expressionsMethods to match URL, phone number, mobile phone, and email address. Notes on methods of using regular expressions to match URL, phone number, mobile phone number, and email addressWhat are they? The following is a practical case. Let’s take a look.

Regular expression, also known as regular expression. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular tables are usually used to retrieve and replace text that matches a certain pattern (rule). Let’s introduce the example code of regular expression matching (URL, phone, mobile phone, email) through example code. Let’s take a look!

Without further ado, I will post the code directly for you. The specific code is as follows:

<!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、电子邮件示例:
//Handsome.W@abc.com
//_hello_world@163.com
//345@mail.some_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>

I believe you will read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Regular replace search keyword highlighting effect of JS

Use of regular expressions in Replace Detailed explanation

The above is the detailed content of Regular expression method to match URL, phone number, mobile phone, and email address. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn