正则表达式的介绍
* 正则表达式也叫规则表达式[ Regular Expression] ,是按照一定的规则组成的一个表达式,主要作用是用来匹配字符串的
* 正则表达式可以在大多数编程语言中使用
* 正则表达式的组成: 由元字符或者限定符组成的一个式子
* . 表示的是: 除换行符( \n) 之外的任意单个字符== == > "word23"
* [ ] 表示的是: 范围,[ 0 - 9 ] 表示的是0 到9 之间任意的一个数== == > "234" ; [ 100 - 199 ] 这种写法是错误的,应该改为[ 1 ] [ 0 - 9 ] [ 0 - 9 ]
* [ a- z] 表示的是: 所有的小写字母中的任意一个; [ A- Z] 表示的是: 所有的大写字母中的任意一个
* [ a- zA- Z] 表示的是: 所有字母中的任意一个; [ 0 - 9 a- zA- Z] 表示的是: 所有数字或者字母中的任意一个
* [ ] 的另一层含义: 把正则表达式中元字符的意义去掉== == > [ . ] 就表示一个点
* | 或者 [ 0 - 9 ] | [ a- z] 表示的是: 要么是一个数字,要么是一个小写的字母
* ( ) 分组,提升优先级 [ 0 - 9 ] | ( [ a- z] ) | [ A- Z]
* ( [ 0 - 9 ] ) ( [ a- z] ) ( [ A- Z] ) 三组,从最左边开始计算
* ( ( ) ( ( ) ) ) 只查左边的半个括号就可以了,共有四组
* * 表示的是: * 前面的表达式出现0 次或者多次
* [ a- z] [ 0 - 9 ] * 表示的是: 小写字母中的任意一个后面要么没有数字,要么有多个数字== == > "word33334"
* 匹配中文的正则表达式 [ \u4e00- \u9fa5]
* + 表示的是: + 前面的表达式出现了1 次到多次
* [ a- z] [ 9 ] + 表示的是: 一个小写字母后面最少有一个9 ,或者多个9 == == > "word9999word"
* ? 表示的是: ? 前面的表达式出现0 次到1 次[ 4 ] [ a- z] ? == == > "1314word"
* {
} 能够更加明确前面的表达式出现的次数
* {
0 , } 表示的是: 前面的表达式出现了0 次到多次,和* 一样
* {
1 , } 表示的是: 前面的表达式出现了1 次到多次,和+ 一样
* {
0 , 1 } 表示的是: 前面的表达式出现了0 次到1 次,和? 一样
* {
5 , 7 } 表示对是: 前面的表达式出现了5 次到7 次
* {
4 } 表示的是: 前面的表达式出现了4 次
* ^ 表示的是以什么开始,或者是取非( 取反) ^ [ 0 - 9 ] == == > "23word" 以数字开头
* ^ [ a- z] 以小写字母开始; [ ^ a- z] 取反,非小写字母; [ ^ 0 - 9 ] 非数字
* [ ^ 0 - 9 a- zA- Z_] 表示的是: 特殊字符,_不算特殊字符
* $ 表示的是: 以什么结束 [ 0 - 9 ] [ a- z] $ 不严格的模式,只要存在就满足== == > "232word" ;
* ^ [ 0 - 9 ] [ a- z] $ 相当于严格模式,每个字符只能出现一次== == > "2b"
* ^ [ 0 - 9 ] [ a- z] 只限定了前面== == > "2f234word"
* \d 表示的是: 数字中的任意一个,相当于[ 0 - 9 ]
* \D 表示的是: 非数字中的任意一个
* \s 表示的是: 空白符中的任意一个( 空格/ tab键)
* \S 表示的是: 非空白符中的任意一个
* \w 表示的是: 非特殊符号中的任意一个
* \W 表示对是: 特殊符号中的任意一个
正则表达式练习
* 根据字符串写正则表达式进行匹配
* 经验: 1. 找规律; 2. 不要追求完美
*
* 身份证的正则表达式: 15 位或者18 位
* ( [ 1 - 9 ] [ 0 - 9 ] {
14 } ) | ( [ 1 - 9 ] [ 0 - 9 ] {
16 } [ 0 - 9 xX] )
* [ 1 - 9 ] [ 0 - 9 ] {
14 } ( [ 0 - 9 ] {
2 } [ 0 - 9 xX] ) ?
*
* 座机号码的正则表达式010 - 19957435 0431 - 46436581
* [ 0 - 9 ] {
3 , 4 } [ - ] [ 0 - 9 ] {
8 }
* \d{
3 , 4 } [ - ] \d{
8 }
*
* QQ号码的正则表达式
* [ 1 - 9 ] [ 0 - 9 ] {
4 - 10 }
*
* 手机号码的正则表达式
* 130 131 132 133 134 135 136 137 138 139
* 143 147 145
* 151 152 159
* 166 167
* 170 174
* 180 181 182 183 184 185 186 187 188 189
* ( [ 1 ] [ 38 ] [ 0 - 9 ] {
9 } ) | ( [ 1 ] [ 4 ] [ 357 ] [ 0 - 9 ] {
8 } ) | ( [ 1 ] [ 5 ] [ 129 ] [ 0 - 9 ] {
8 } ) | ( [ 1 ] [ 6 ] [ 67 ] [ 0 - 9 ] {
8 } ) | ( [ 1 ] [ 7 ] [ 04 ] [ 0 - 9 ] {
8 } )
*
* 邮箱的正则表达式
* 2483762760 @qq . com
* ww2483762760@itcast . com. cn
* [ 0 - 9 a- zA- Z. _- ] + [ @] [ 0 - 9 a- zA- Z. _- ] + ( [ . ] [ a- zA- Z] + ) {
1 , 2 }
正则表达式的创建
var reg1= new RegExp ( / \d{
7 } / ) ;
var reg2= new RegExp ( "\\d{7}" ) ;
var str1= "那是我日夜思念深深爱着的人啊1314521" ;
console. log ( reg1. test ( str1) ) ;
var reg= new RegExp ( / ^ \d{
7 } $/ ) ;
var str= "那是我日夜思念深深爱着的人啊1314521" ;
console. log ( reg. test ( str) ) ;
var reg= / \d{
1 , 4 } / ;
var flag= reg. test ( "我曾把完整的镜子打碎,夜晚的枕头都是眼泪8848" ) ;
console. log ( flag) ;
正则表达式结果的识别
console. log ( / . /. test ( "除了回车换行以为的任意字符" ) ) ;
console. log ( / . */ . test ( "0个到多个" ) ) ;
console. log ( / . +/ . test ( "1个到多个" ) ) ;
console. log ( / . ?/ . test ( "哈哈" ) ) ;
console. log ( / [ 0 - 9 ] / . test ( "9527" ) ) ;
console. log ( / [ a- z] / . test ( "what" ) ) ;
console. log ( / [ A- Z] / . test ( "Are" ) ) ;
console. log ( / [ a- zA- Z] / . test ( "干啥子" ) ) ;
console. log ( / [ 0 - 9 a- zA- Z] / . test ( "9ebg" ) ) ;
console. log ( / b| ( ara) / . test ( "abra" ) ) ;
console. log ( / [ a- z] {
2 , 3 } / . test ( "word" ) ) ;
console. log ( / \d/ . test ( "998" ) ) ;
console. log ( / \d* / . test ( "998" ) ) ;
console. log ( / \d+ / . test ( "998" ) ) ;
console. log ( / \d{
0 , } / . test ( "998" ) ) ;
console. log ( / \d{
2 , 3 } / . test ( "998" ) ) ;
console. log ( / \D/ . test ( "eat" ) ) ;
console. log ( / \s/ . test ( " " ) ) ;
console. log ( / \S/ . test ( "嘎嘎 " ) ) ;
console. log ( / \w/ . test ( "_" ) ) ;
console. log ( / \W/ . test ( "_" ) ) ;
正则表达式案例
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< style type= "text/css" >
#dv {
width: 230 px;
height: 200 px;
margin: 200 px auto;
border: 1 px solid blue;
}
. strengthLv0 {
height: 6 px;
width: 120 px;
border: 1 px solid #ccc;
padding: 2 px;
margin- left: 36 px;
}
. strengthLv1 {
background: red;
height: 6 px;
width: 40 px;
border: 1 px solid #ccc;
padding: 2 px;
margin- left: 36 px;
}
. strengthLv2 {
background: orange;
height: 6 px;
width: 80 px;
border: 1 px solid #ccc;
padding: 2 px;
margin- left: 36 px;
}
. strengthLv3 {
background: green;
height: 6 px;
width: 120 px;
border: 1 px solid #ccc;
padding: 2 px;
margin- left: 36 px;
}
< / style>
< / head>
< body>
< div id= "dv" >
< label for = "pwd" > 密码< / label>
< input type= "text" id= "pwd" maxlength= "16" >
< div>
< div id= "strengthLevel" class = "strengthLv0" > < / div>
< / div>
< / div>
< script>
document. getElementById ( "pwd" ) . onkeyup = function ( ) {
if ( this . value. length>= 6 ) {
var level= getLevel ( this . value) ;
document. getElementById ( "strengthLevel" ) . className= "strengthLv" + level;
} else {
document. getElementById ( "strengthLevel" ) . className= "strengthLv0" ;
}
} ;
function getLevel ( value) {
var level= 0 ;
if ( / \d/ . test ( value) ) {
level++ ;
}
if ( / [ a- zA- Z] / . test ( value) ) {
level++ ;
}
if ( / \W/ . test ( value) ) {
level++ ;
}
return level;
}
< / script>
< / body>
< / html>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< / head>
< body>
请输入你的名字: < input type= "text" value= "" id= "userName" > * < br/ >
< script>
document. getElementById ( "userName" ) . onblur= function ( ) {
var reg= / ^ [ \u4e00- \u9fa5] {
2 , 6 } $/ ;
if ( reg. test ( this . value) ) {
this . style. backgroundColor= "green" ;
} else {
this . style. backgroundColor= "blue" ;
}
} ;
< / script>
< / body>
< / html>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< / head>
< body>
请输入邮箱地址: < input type= "text" value= "" id= "email" / > * < br/ >
< script>
document. getElementById ( "email" ) . onblur= function ( ) {
var reg= / ^ [ 0 - 9 a- zA- Z. _- ] + [ @] [ 0 - 9 a- zA- z. _- ] + ( [ . ] [ a- zA- Z] + ) {
1 , 2 } $/ ;
if ( reg. test ( this . value) ) {
this . style. backgroundColor= "green" ;
} else {
this . style. backgroundColor= "blue" ;
}
} ;
< / script>
< / body>
< / html>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Document< / title>
< style type= "text/css" >
body {
background: #ccc;
}
label {
width: 40 px;
display: inline- block;
}
span {
color: red;
}
. container {
margin: 100 px auto;
width: 400 px;
padding: 50 px;
line- height: 40 px;
border: 1 px solid #999 ;
background: #efefef;
}
span {
margin- left: 30 px;
font- size: 12 px;
}
< / style>
< / head>
< body>
< div class = "container" id= "dv" >
< label for = "qq" > Q Q< / label> < input type= "text" id= "qq" > < span> < / span> < br/ >
< label> 手机< / label> < input type= "text" id= "phone" > < span> < / span> < br/ >
< label> 邮箱< / label> < input type= "text" id= "e-mail" > < span> < / span> < br/ >
< label> 座机< / label> < input type= "text" id= "telephone" > < span> < / span> < br/ >
< label> 姓名< / label> < input type= "text" id= "fullName" > < span> < / span> < br/ >
< / div>
< script>
checkInput ( document. getElementById ( "qq" ) , / ^ [ 1 - 9 ] \d{
4 , 10 } $/ ) ;
checkInput ( document. getElementById ( "phone" ) , / ^ \d{
11 } $/ ) ;
checkInput ( document. getElementById ( "e-mail" ) , / ^ [ 0 - 9 a- zA- Z- . _] + [ @] + [ 0 - 9 a- zA- Z- . _] + ( [ . ] [ a- zA- Z] ) {
1 , 2 } $/ ) ;
checkInput ( document. getElementById ( "telephone" ) , / ^ \d{
3 , 4 } [ - ] \d{
7 , 8 } $/ ) ;
checkInput ( document. getElementById ( "fullName" ) , / ^ [ \u4e00- \u9fa5] {
2 , 6 } $/ ) ;
function checkInput ( input, reg) {
input. onblur= function ( ) {
if ( reg. test ( this . value) ) {
this . nextElementSibling. innerText= "正确了" ;
console. log ( "222" ) ;
this . nextElementSibling. style. color= "blue" ;
} else {
this . nextElementSibling. innerText= "傻逼" ;
this . nextElementSibling. style. color= "red" ;
}
} ;
}
< / script>
< / body>
< / html>
正则表达式其他方法的使用
< script>
var str1 = "中国移动:10086,中国联通:10010,中国电信:10000" ;
var array1= str1. match ( / \d{
5 } / g) ;
console. log ( array1) ;
var reg= / \d{
5 } / g;
ary= reg. exec ( str1) ;
console. log ( ary) ;
console. log ( reg. exec ( str1) ) ;
console. log ( reg. exec ( str1) ) ;
console. log ( reg. exec ( str1) ) ;
var result= reg. exec ( str1) ;
while ( result!= null) {
console. log ( result[ 0 ] ) ;
result= reg. exec ( str1)
}
var str2 = "[email protected] ,[email protected] [email protected] 2、[email protected] [email protected] ..." ;
var array2 = str2. match ( / [ 0 - 9 a- zA- Z. _- ] + [ @] [ 0 - 9 a- zA- Z. _- ] + ( [ . ] [ a- zA- Z] + ) {
1 , 2 } / g) ;
console. log ( array2) ;
var str3= "2019-03-27" ;
var array3= str3. match ( / ( \d{
4 } ) [ - ] ( \d{
2 } ) [ - ] ( \d{
2 } ) / ) ;
console. log ( RegExp. $1 ) ;
console. log ( RegExp. $2 ) ;
console. log ( RegExp. $3 ) ;
var str4= "那是我日夜思念深深爱着的人啊" ;
console. log ( str4. replace ( / [ 是] / , "不是" ) ) ;
var str5= "word hello Word" ;
var re = new RegExp ( / [ w] / , "gi" ) ;
var re2 = new RegExp ( / [ w] / gi) ;
console. log ( str5. replace ( / \s+ / g, "" ) ) ;
console. log ( str5. replace ( / [ w] / gi, "S" ) ) ;
console. log ( str5. replace ( re, "S" ) ) ;
console. log ( str5. replace ( re2, "S" ) ) ;
< / script>