javascript正则表达式的运用

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2  <html xmlns=" http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>无标题文档</title>
6 <script type="text/javascript">
7 function submit()
8 {
9 var arr=document.getElementById('name').value;
10 var email=document.getElementById('email').value;
11 if(arr||email)
12 {
13 var demo=/^[a-z][0-9a-z]{6,18}/i;
14 var demoa=/^[0-9a-z]{4,}@[0-9a-z]{2,}\.(com|cn|net)/;
15 if(arr.match(demo))
16 {
17 if(!email.match(demoa))
18 {
19 alert("不是有效地邮箱!");
20 }
21 }
22 else
23 {
24 alert('该项必须为6~18位字符且以字母开头不能为汉字!!')
25 }
26 }
27 else
28 {
29 if(arr=="")
30 {
31 alert("姓名不能为空!")
32 }
33 else if(email=="")
34 {
35 alert("email不能为空!")
36 }
37 }
38 }
39 </script>
40 </head>
41
42 <body>
43 姓名:<input type="text" id="name"><br/>
44 email;<input type="text" id="email"><br/>
45 <input type="button" value="提交" name="button" onclick="submit()"/>
46 </body>
47 </html>

总结:

正则表达式由原子,元字符,定界符,以及模式修正符构成,以需要匹配的字符串来编写合理的正则表达式;

原文地址:https://www.cnblogs.com/mizzle/p/1892643.html