创建正则表达式的语法:
第一种方法:
var reg = /d/; //双斜杠包起来
第二种方法:
var reg = new RegExp('pattern');//定义一个对象
RegExp对象的常用方法:
exec(str) 检索字符串中指定的值。返回找到的值,并确认其位置。
test(str) 检索字符串中的是否有满足指定条件的值。返回true或false。
(/D/g,"") 为正则表达式,用来匹配所有非数字字符;输入值为非数字的字符替换为空;
replace(),它使用正则替换不符合规范的字符
正则替换中文字符(不能输入中文)
.replace(/[u4e00-u9fa5]/g, "")
正则替换空字符串:(空)
.replace(/^(s|u00A0)+|(s|u00A0)+$/g, "")
过滤数字(不能输入非数字,只能输入数字)
.replace(/D/g, "")
只能输入中文
.replace(/[^u4e00-u9fa5]/g, "")
表达式:
i代表小写;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /abc/i; var str = "ABCd"; </script> </body> </html>
效果图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /^a/g; var str = "abcde a"; </script> </body> </html>
效果图:以a开头的这个a
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /^a/gm; var str = "abcde a"; </script> </body> </html>
效果图:m代表多行, 换行的意思
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /[1234567890][1234567890][1234567890]/g; var str = "123fhjhjhgf25648jfdkfhdh"; </script> </body> </html>
效果图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /[^a][^b]/g; var str = "ab1cd"; </script> </body> </html>
效果图:[]代表一位,g代表全局,注意:^尖角号放在表达式里代表非[^]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /(abc|bcd)[0-9]/g; var str = "bcd2"; </script> </body> </html>
效果图:|代表或的意思
字符串:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /wcd2/g; var str = "bcd2"; var reg2 = /Wcd2/g; var str2 = "bcd2"; var reg3 = /Wcd2/g; var str3 = "b*cd2"; </script> </body> </html>
效果图: w === [0-9A-z_] ; W===[^w];
d === [0-9] ;
D === [^d];
s === [ f ]; 查找空白字符
S === [^s]; 查找非空白字符
=== 单词边界;
B === 非单词边界;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /cbeB/g; var str = "abc cbebdg"; </script> </body> </html>
效果图:
查找制表符;(其实是Tab键效果)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = / c/g; var str = "abc cbebdg"; </script> </body> </html>
效果图:
查找换行符
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = / c/g; var str = "abc cbebdg"; </script> </body> </html>
效果图:
unicode字符:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /[u0000-uffff]/g; var str = "老邓身体好"; </script> </body> </html>
效果图:[u0000-uffff]代表一切字符,还有[dD]、[sS]等一个小一个大的,代表一切字符
. === [^ ]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <script type="text/javascript"> var reg = /./g; var str = "老邓身体好"; </script> </body> </html>
效果图同上: