什么是 RegExp?
RegExp 是正则表达式的缩写。
当您检索某个文本时,可以使用一种模式来描述要检索的内容。RegExp 就是这种模式。
简单的模式可以是一个单独的字符。
更复杂的模式包括了更多的字符,并可用于解析、格式检查、替换等等。
您可以规定字符串中的检索位置,以及要检索的字符类型,等等。
定义 RegExp
RegExp 对象用于存储检索模式。
通过 new 关键词来定义 RegExp 对象。以下代码定义了名为 patt1 的 RegExp 对象,其模式是 “e”:
let patt1=new RegExp("e");
RegExp 对象的方法
RegExp 对象有 3 个方法:test()、exec() 以及 compile()。
test() 方法检索字符串中的指定值。返回值是 true 或 false。
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
//由于该字符串中存在字母 "e",以上代码的输出将是:
//true
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
//由于该字符串中存在字母 "e",以上代码的输出将是:
//e
compile() 方法用于改变 RegExp。
compile() 既可以改变检索模式,也可以添加或删除第二个参数。
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
patt1.compile("d");
document.write(patt1.test("The best things in life are free"));
//由于字符串中存在 "e",而没有 "d",以上代码的输出是:
//truefalse
方括号
方括号用于查找某个范围内的字符:
1.[abc] 查找方括号之间的任何字符。
reg.compile("[abc]");
document.write("<h2>[abc]"+reg.exec("Abcd")+"</h2>");
2.[0-9] 查找任何从 0 至 9 的数字。
reg.compile("[0-9]");
document.write("<h2>[0-9]"+reg.exec("Abcd3")+"</h2>");
3.[a-z] 查找任何从小写 a 到小写 z 的字符。
reg.compile("[a-z]");
document.write("<h2>[a-z]小写字母"+reg.exec("Abcd3")+"</h2>");
4.[A-Z] 查找任何从大写 A 到大写 Z 的字符。
reg.compile("[A-Z]");
document.write("<h2>[A-Z]大写字母"+reg.exec("Abcd3")+"</h2>");
5.[A-z] 查找任何从大写 A 到小写 z 的字符。
reg.compile("[A-z]");
document.write("<h2>[A-z]单词字符"+reg.exec("_Abcd3")+"</h2>");
6.(red|blue|green) 查找任何指定的选项。
reg.compile("[ab|ac|ad]");
document.write("<h2>[ab|ac|ad]单词字符"+reg.exec("_Abcd3")+"</h2>");
元字符
元字符(Metacharacter)是拥有特殊含义的字符:
1 . 查找单个字符,除了换行和行结束符。
document.write("<h2>姓名:"+"张三".match("^张.")+"</h2>");
document.write("<h2>QQ邮箱:"+"12345678@qq.com".match("^\\d{6,10}@qq\\.com$")+"</h2>");
2 \w 查找单词字符。
document.write("<h2>英文名:"+"greenMan".match("^\\w{3,}$")+"</h2>");
3 \W 查找非单词字符。
document.write("<h2>中文名:"+"张三丰".match("^\\W{3,}$")+"</h2>");
4 \d 查找数字。
document.write("<h2>手机号:"+"12345678901".match("^1\\d{10}$")+"</h2>");
5 \D 查找非数字字符。
document.write("<h2>非数字:"+"a".match("^\\D$")+"</h2>");
支持正则表达式的 String 对象的方法
match 找到一个或多个正则表达式的匹配。
实列如下:
document.write("<h2>至少一个:"+"12".match("^[0-9]+$")+"</h2>");
document.write("<h2>零或多个:"+"12".match("^[0-9]*$")+"</h2>");
document.write("<h2>零或一个:"+"1".match("^[0-9]?$")+"</h2>");
document.write("<h2>正好n个:"+"1234".match("^[0-9]{4}$")+"</h2>");
document.write("<h2>手机号:"+"12345678901".match("^1[0-9]{10}$")+"</h2>");
document.write("<h2>最少x个最多y个:"+"123".match("^[0-9]{3,6}$")+"</h2>");
document.write("<h2>QQ号:"+"12345678".match("^[0-9]{6,10}$")+"</h2>");
document.write("<h2>QQ邮箱:"+"12345678@qq.com".match("^[0-9]{6,10}@qq.com$")+"</h2>");
document.write("<h2>身份证:"+"12345678901234567x".match("^[0-9]{17}[0-9x]$")+"</h2>");
document.write("<h2>英文名:"+"greenMan".match("^[A-z]{3,}$")+"</h2>");
document.write("<h2>英文名:"+"greenMan".match("^\\w{3,}$")+"</h2>");
document.write("<h2>中文名:"+"张三丰".match("^\\W{3,}$")+"</h2>");
document.write("<h2>手机号:"+"12345678901".match("^1\\d{10}$")+"</h2>");
document.write("<h2>非数字:"+"a".match("^\\D$")+"</h2>");
document.write("<h2>姓名:"+"张三".match("^张.")+"</h2>");
document.write("<h2>QQ邮箱:"+"12345678@qq.com".match("^\\d{6,10}@qq\\.com$")+"</h2>");