正则表达式
RegExp 对象
定义 RegExp
RegExp 对象用于存储检索模式。
通过 new 关键词来定义 RegExp 对象。以下代码定义了名为 patt1 的 RegExp 对象,其模式是 “e”:
var patt1=new RegExp("e");
当您使用该 RegExp 对象在一个字符串中检索时,将寻找的是字符 “e”。
RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具。
RegExp 对象的方法
RegExp 对象有 3 个方法:test()、exec() 以及 compile()。
test()
test() 方法检索字符串中的指定值。返回值是 true 或 false。
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
exec()
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
compile()
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"));
创建 RegExp 对象的语法:
new RegExp(pattern, attributes);
方括号
方括号用于查找某个范围内的字符:
表达式 | 描述 |
---|---|
[abc] | 查找方括号之间的任何字符。 |
[^abc] | 查找任何不在方括号之间的字符。 |
[0-9] | 查找任何从 0 至 9 的数字。 |
[a-z] | 查找任何从小写 a 到小写 z 的字符。 |
[A-Z] | 查找任何从大写 A 到大写 Z 的字符。 |
[A-z] | 查找任何从大写 A 到小写 z 的字符。 |
[adgk] | 查找给定集合内的任何字符。 |
[^adgk] | 查找给定集合外的任何字符。 |
(red|blue|green) | 查找任何指定的选项。 |
元字符
元字符(Metacharacter)是拥有特殊含义的字符:
元字符 | 描述 |
---|---|
. | 查找单个字符,除了换行和行结束符。 |
\w | 查找单词字符。 |
\W | 查找非单词字符。 |
\d | 查找数字。 |
\D | 查找非数字字符。 |
\s | 查找空白字符。 |
\S | 查找非空白字符。 |
\b | 匹配单词边界。 |
\B | 匹配非单词边界。 |
\0 | 查找 NUL 字符。 |
\n | 查找换行符。 |
\f | 查找换页符。 |
\r | 查找回车符。 |
\t | 查找制表符。 |
\v | 查找垂直制表符。 |
\xxx | 查找以八进制数 xxx 规定的字符。 |
\xdd | 查找以十六进制数 dd 规定的字符。 |
\uxxxx | 查找以十六进制数 xxxx 规定的 Unicode 字符。 |
量词
量词 | 描述 |
---|---|
n+ | 匹配任何包含至少一个 n 的字符串。 |
n* | 匹配任何包含零个或多个 n 的字符串。 |
n? | 匹配任何包含零个或一个 n 的字符串。 |
n{X} | 匹配包含 X 个 n 的序列的字符串。 |
n{X,Y} | 匹配包含 X 至 Y 个 n 的序列的字符串。 |
n{X,} | 匹配包含至少 X 个 n 的序列的字符串。 |
n$ | 匹配任何结尾为 n 的字符串。 |
^n | 匹配任何开头为 n 的字符串。 |
?=n | 匹配任何其后紧接指定字符串 n 的字符串。 |
?!n | 匹配任何其后没有紧接指定字符串 n 的字符串。 |
[abc] 表达式:查找方括号之间的任何字符。
reg.compile("[abc]");
document.write("<h2>[abc]:"+reg.exec("Abcd")+"</h2>");
<script>
let str="hello word!";
// 1.创建正则对象
let reg=new RegExp("a");
// test() 返回true false
document.write("<h2>在str中查找a是否存在:"+reg.test(str)+"</h2>");
// 返回值是被找到的值
document.write("<h2>在str中查找a是否存在:"+reg.exec("bbcd")+"</h2>");
// 改变检索规则
reg.compile("d");
document.write("<h2>在str中查找d是否存在:"+reg.exec("bbcd")+"</h2>");
// 忽略大小写
reg.compile("a",'i');
document.write("<h2>在str中查找a是否存在:"+reg.exec("Abcd")+"</h2>");
// 方括号用于查找某个范围内的字符
reg.compile("[abc]");
document.write("<h2>[abc]:"+reg.exec("Abcd")+"</h2>");
reg.compile("[0-9]");
document.write("<h2>[0-9]:"+reg.exec("Abcd3")+"</h2>");
reg.compile("[a-z]");
document.write("<h2>[a-z]小写字母:"+reg.exec("Abcd3")+"</h2>");
reg.compile("[A-Z]");
document.write("<h2>[A-Z]大写字母:"+reg.exec("Abcd3")+"</h2>");
reg.compile("[A-z]");
document.write("<h2>[A-z]单词字符:"+reg.exec("-_Abcd3")+"</h2>");
reg.compile("[ab|ac|ad]");
document.write("<h2>[ab|ac|ad]单词字符:"+reg.exec("_Abcd3")+"</h2>");
// name 张王李赵
reg.compile("[张王李赵]");
document.write("<h2>[张王李赵]单词字符:"+reg.exec("张王李赵")+"</h2>");
reg.compile("[0-35]");//0,1,2,3,5 0-3,5
document.write("<h2>[0-35]:"+reg.exec("7")+"</h2>");
// 查找姓名含有 三,年,五的人
// 账号中有没有英文字符 [A-z]
// 年龄的格式是否正确 [0-9]
// 手机号
reg.compile("1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]");
document.write("<h2>手机号:"+reg.exec("12345678914a")+"</h2>");
document.write("<h2>手机号:"+
"a12345678914".match("^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$")
+"</h2>");
// 量词
// 含至少一个
document.write("<h2>至少一个:"+"123".match("^[0-9]+$")+"</h2>");
document.write("<h2>零个或多个:"+"a".match("^[0-9]*$")+"</h2>");
document.write("<h2>零个或一个:"+"1".match("^[0-9]?$")+"</h2>");
document.write("<h2>正好n个:"+"123".match("^[0-9]{3}$")+"</h2>");
document.write("<h2>手机号:"+"12345678914".match("^1[0-9]{10}$")+"</h2>");
document.write("<h2>最少x个最多y个:"+"123".match("^[0-9]{3,6}$")+"</h2>");
document.write("<h2>QQ号:"+"235678".match("^[0-9]{6,10}$")+"</h2>");
// 验证qq邮箱 1234313@qq.com
document.write("<h2>QQ邮箱:"+"289456@qq.com".match("^[0-9]{6,10}@qq.com$")+"</h2>")
// 身份证 17数字x 18数字
document.write("<h2>身份证"+"12345678909876543X".match("^[0-9]{17}[0-9X]$")+"</h2>")
// 元字符 \\w[A-z0-9]
document.write("<h2>英文名"+"greenMan".match("^\\w{3,}$")+"</h2>");
document.write("<h2>中文名"+"张三丰".match("^\\w{3,}$")+"</h2>");
// \\d [0-9]
document.write("<h2>手机号"+"12345678910".match("^1\\d{10}$")+"</h2>");
document.write("<h2>非数字"+"a".match("^\\D$")+"</h2>");
// . 任意字符 张XXX
document.write("<h2>姓名"+"张三是".match("^张..$")+"</h2>");
document.write("<h2>QQ邮箱:"+"289456@qq.com".match("^\\d{6,10}@qq\\.com$")+"</h2>")
</script>