正则表达式简明教程!
简介和实例
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来提取一大段字符串中,含有的特定格式子字符串。正则表达式是由普通字符以及特殊字符组成的文字模式。
1、提取数字部分
# 从字符串"abc123def"中,提取出数字部分
var str = "abc123def";
var patt1 = /[0-9]+/;
document.write(str.match(patt1));
# 输出结果:123
2、找出相邻且相同单词
# Is is the cost of of gasoline going up up?
# 找出上面字符串中所有相邻两个单词是相同的字符(不区分大小写)
var str = "Is is the cost of of gasoline going up up";
var patt1 = /([a-z]+) 1/ig;
document.write(str.match(patt1));
# 结果
Is is
of of
up up
# 说明
两个表明了一个单词边界;
[a-z]+ 表示一个单词;
([a-z]+) 会匹配字符串中所有单词并存储起来;
1 表示访问上面存储的第一个单词;
3、url识别
var str = "http://www.runoob.com:80/html/html-tutorial.html";
var patt1 = /(w+)://([^/:]+)(:d*)?([^# ]*)/;
arr = str.match(patt1);
for (var i = 0; i < arr.length ; i++) {
document.write(arr[i]);
document.write("<br>");
}
4、正则表达式的两种使用方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>smallpdf.cn</title>
</head>
<body>
<script>
// (patt1 等同于 patt2)正则表达式的两种使用方式
var str = "Is is the cost of of gasoline going up up";
var patt1 = /([a-z]+) 1/ig;
document.write("实例1:", str.match(patt1));
document.write("<br><br>");
var patt2 = new RegExp("\b([a-z]+) \1\b", "ig")
document.write("实例2:"+str.match(patt2));
</script>
</body>
</html>
5、全局与非全局匹配
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>smallpdf.cn</title>
</head>
<body>
<script>
var str = "Google smallpdf.cn taobao smallpdf.cn";
var n1 = str.match(/smallpdf.cn/); // 查找第一次匹配项
var n2 = str.match(/smallpdf.cn/g); // 查找所有匹配项
document.write("实例1:", n1);
document.write("<br><br>");
document.write("实例2:", n2);
</script>
</body>
</html>
6、匹配E-Mail(邮箱)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>smallpdf.cn</title>
</head>
<body>
<script>
var str = "abcd test@runoob.com 1234";
var patt1 = /[w.%+-]+@[w.-]+.[a-zA-Z]{2,6}/g;
document.write(str.match(patt1));
</script>
</body>
</html>
7、动手验证实践一下
正则语法
1、定位符
定位符可将正则表达式固定在:行首、行尾、一个单词内、单词开头、单词结尾。定位符不能跟限定符一起使用,譬如:^*
这是错误的,因为 一个字符串只有1个开始,不存在0个或多个开始。
正则 | 含义 | 字符串 | 正则表达式 | 结果 |
---|---|---|---|---|
| 表示字符串的开始 | "An E" |
| 'A' |
| 表示字符串的结束 | "eat" |
| 't' |
单词的前后边界 | “moon” |
| ‘m’(查找m开头单词) | |
| 单词的非边界部分 | "noonday" |
| 'oo'(单词中包含oo且不在单词边界) |
| 正则表达式的终止符 | |||
|
2、普通字符
正则 | 含义 | 字符串 | 正则表达式 | 结果 |
---|---|---|---|---|
| 匹配一个数字,等价于 | "B2 is the suite number." |
| '2' |
| 匹配一个非数字字符,等价于 | "B2 is the suite number." |
| 'B' |
| 匹配一个字符(数字、字母、下划线),等同 | "apple," |
| 'a' |
| 匹配一个字符,等价于 | "50%." |
| '%' |
| 匹配一个空白字符(空格、制表符、换页符、换行符) | "foo bar." |
| ' bar' |
| 匹配一个非空白字符 | "foo bar." |
| 'foo' |
| 匹配任一字符,换行符(
、
)除外,等同 | "nay, an apple is on the tree" |
| 'an'、'on' |
| 匹配a、b、c中任一字符, | "asdfiobab" |
| 'a'、'b'、'a'、'b' |
| 不包含a、b、c的所有字符 | |||
| 匹配A到Z中任一字符 | |||
| 匹配a到z中任一字符 | |||
| 匹配0到9中任一数字 |
3、限定符
正则 | 含义 | 字符串 | 正则表达式 | 匹配结果 |
---|---|---|---|---|
| 匹配 0 或 1 次
等同 | "angel" |
| 'el' |
| 匹配 0 次或多次
等同 | "<p>smallpdf.cn</p>" |
| '<p>smallpdf.cn</p>' |
| 消除贪婪,匹配尽可能少 | "<p>smallpdf.cn<p>" |
| '<p>' 和 '</p>' |
| 匹配次数≥1,等同 | "<p>smallpdf.cn</p>" |
| '<p>smallpdf.cn</p>' |
| 消除贪婪,匹配尽可能少 | "<p>smallpdf.cn</p>" |
| '<p>' 和 '</p>' |
| n是正整数,匹配次数 = n | |||
| n是正整数,匹配次数 ≥ n | |||
| n和m都是整数 n ≤ 匹配次数 ≤ m n或m为0,忽略 |
4、逻辑运算
正则 | 含义 | 字符串 | 正则表达式 | 匹配结果 |
---|---|---|---|---|
| 匹配 x 或 y | "red apple" |
| 'red' |
| 匹配x,并存储匹配值,
| 看下面实例 | ||
| 返回第num个缓存值,num是整数从1开始。 | "apple, orange, cherry, peach." |
| 'apple, orange,' |
| 匹配x,但不存匹配字符,
| |||
| 匹配 x 且后面是 y,不存匹配值 | "JackSpa" |
| 'Jack' |
| 匹配 x 且后面不是 y,不存匹配值 | "JackSp" |
| 'Jack' |
| 匹配 x 且前面是 y,不存匹配值 | "JackSpa" |
| 'Spa' |
| 匹配 x 且前面不是 y ,不存匹配值 | "JacSpa" |
| 'Spa' |
5、非打印字符
正则 | 含有 |
---|---|
| 匹配一个退格(U+0008) |
| 匹配一个换页符 (U+000C) |
| 匹配一个换行符 (U+000A) |
| 匹配一个回车符 (U+000D) |
| 匹配一个水平制表符 (U+0009) |
| 匹配一个垂直制表符 (U+000B) |