<span style=
"color: #008080"
> 1</span> <span style=
"font-size: 15px"
><span style=
"color: #000000"
>1. 正则表达式:规定字符串中字符出现规律的规则。
</span><span style=
"color: #008080"
> 2</span> <span style=
"color: #000000"
> 何时使用:查找 验证 替换 切割
</span><span style=
"color: #008080"
> 3</span> <span style=
"color: #000000"
> 模糊查找,验证字符串格式,模糊替换,按照一定规律切割字符串
</span><span style=
"color: #008080"
> 4</span>
<span style=
"color: #008080"
> 5</span> <span style=
"color: #000000"
> 正则表达式语法:
</span><span style=
"color: #008080"
> 6</span> <span style=
"color: #000000"
> 1. 最简单正则:关键字正文
</span><span style=
"color: #008080"
> 7</span> <span style=
"color: #000000"
> 2. 备选字符集:字符串中某*一位*上可选的字符的集合
</span><span style=
"color: #008080"
> 8</span> <span style=
"color: #000000"
> [备选字符列表]
</span><span style=
"color: #008080"
> 9</span> <span style=
"color: #000000"
> 强调:一个备选字符集,只能规定一位字符
</span><span style=
"color: #008080"
> 10</span> <span style=
"color: #000000"
> 如果备选字符列表是(部分)连续的:用-省略中间字符
</span><span style=
"color: #008080"
> 11</span> <span style=
"color: #000000"
> 仅表示*除别字符外*,所有字符均可:^除掉字符——了解
</span><span style=
"color: #008080"
> 12</span> <span style=
"color: #000000"
> 3. 预定义字符集:对常用字符集的简写方式
</span><span style=
"color: #008080"
> 13</span> <span style=
"color: #000000"
> \d:1位数字 [0-9]
</span><span style=
"color: #008080"
> 14</span> <span style=
"color: #000000"
> \w: 1位数字,字母或下划线 [0-9a-zA-Z_]
</span><span style=
"color: #008080"
> 15</span> <span style=
"color: #000000"
> \s: 1位空字符:空格 tab 回车换行
</span><span style=
"color: #008080"
> 16</span>
<span style=
"color: #008080"
> 17</span> <span style=
"color: #000000"
> . : 1位任意字符,除回车换行外
</span><span style=
"color: #008080"
> 18</span> <span style=
"color: #000000"
> 预定义字符集 vs 自定义字符集
</span><span style=
"color: #008080"
> 19</span> <span style=
"color: #000000"
> 预定义字符集语法简单,规则是固定的,不可变!
</span><span style=
"color: #008080"
> 20</span> <span style=
"color: #000000"
> 自定义字符集,规则完全自定义,灵活!语法繁琐
</span><span style=
"color: #008080"
> 21</span>
<span style=
"color: #008080"
> 22</span> <span style=
"color: #000000"
> 4. 数量词:规定一个字符集出现次数的量词
</span><span style=
"color: #008080"
> 23</span> <span style=
"color: #000000"
> 如何使用:紧跟字符集之后,修改相邻的前一个字符集
</span><span style=
"color: #008080"
> 24</span> <span style=
"color: #000000"
> 1. 确定数量:3种:
</span><span style=
"color: #008080"
> 25</span> <span style=
"color: #000000"
> {m,n} 最少m个,最多n个
</span><span style=
"color: #008080"
> 26</span> <span style=
"color: #000000"
> {m,} m个以上
</span><span style=
"color: #008080"
> 27</span> <span style=
"color: #000000"
> {m} 必须m个
</span><span style=
"color: #008080"
> 28</span> <span style=
"color: #000000"
> 2. 不确定数量:3种:
</span><span style=
"color: #008080"
> 29</span> <span style=
"color: #000000"
> ?: 可有可无,最多一个
</span><span style=
"color: #008080"
> 30</span> <span style=
"color: #000000"
> *: 可有可无,多了不限
</span><span style=
"color: #008080"
> 31</span> <span style=
"color: #000000"
> +: 至少一个,多了不限
</span><span style=
"color: #008080"
> 32</span> <span style=
"color: #000000"
> 5. 选择和分组:
</span><span style=
"color: #008080"
> 33</span> <span style=
"color: #000000"
> 分组: (子表达式)
</span><span style=
"color: #008080"
> 34</span> <span style=
"color: #000000"
> 选择:或 表达式1|表达式2
</span><span style=
"color: #008080"
> 35</span>
<span style=
"color: #008080"
> 36</span> <span style=
"color: #000000"
> 手机号:+86或0086 可有可无,最多一个
</span><span style=
"color: #008080"
> 37</span> <span style=
"color: #000000"
> 空字符 可有可无,多了不限
</span><span style=
"color: #008080"
> 38</span> <span style=
"color: #000000"
> 1
</span><span style=
"color: #008080"
> 39</span> <span style=
"color: #000000"
> 3,4,5,7,8 选其一
</span><span style=
"color: #008080"
> 40</span> <span style=
"color: #000000"
> 9位数字,不能多,不能少
</span><span style=
"color: #008080"
> 41</span> <span style=
"color: #000000"
> (\+86|0086)?\s*1[34578]\d{9}
</span><span style=
"color: #008080"
> 42</span> <span style=
"color: #000000"
> 身份证号:15位数字
</span><span style=
"color: #008080"
> 43</span> <span style=
"color: #000000"
> 2位数字
</span><span style=
"color: #008080"
> 44</span> <span style=
"color: #000000"
> 从数字、x、X中选一个
</span><span style=
"color: #008080"
> 45</span> <span style=
"color: #000000"
> 后两部分整体可有可无,最多一次
</span><span style=
"color: #008080"
> 46</span> <span style=
"color: #000000"
> \d{15}(\d\d[0-9xX])?
</span><span style=
"color: #008080"
> 47</span> 匹配img元素:<span style=
"color: #0000ff"
><</span><span style=
"color: #800000"
>img </span><span style=
"color: #ff0000"
>xxxx src</span><span style=
"color: #0000ff"
>=
""
</span><span style=
"color: #ff0000"
> xxxx </span><span style=
"color: #0000ff"
>/></span>
<span style=
"color: #008080"
> 48</span> <span style=
"color: #0000ff"
><</span><span style=
"color: #800000"
>img </span><span style=
"color: #ff0000"
>1位以上任意字符 src</span><span style=
"color: #0000ff"
>= 在
'"间选一个 </span><span style="color: #ff0000">1位以上任意字符 在'
"间选一个 1位以上任意字符 </span><span style=
"color: #0000ff"
>/></span>
<span style=
"color: #008080"
> 49</span> <span style=
"color: #000000"
> 贪婪模式:默认匹配最长的字符
</span><span style=
"color: #008080"
> 50</span> <span style=
"color: #000000"
> 比如: .* .+
</span><span style=
"color: #008080"
> 51</span> <span style=
"color: #000000"
> 懒惰模式:仅匹配最短的字符
</span><span style=
"color: #008080"
> 52</span> <span style=
"color: #000000"
> 贪婪改懒惰:.*=>(.*?) .+==>(.+?)
</span><span style=
"color: #008080"
> 53</span> <span style=
"color: #0000ff"
><</span><span style=
"color: #800000"
>img</span><span style=
"color: #ff0000"
>(.+?)src</span><span style=
"color: #0000ff"
>=[
'"](.+?)['
"](.+?)</span><span style=
"color: #0000ff"
>/></span>
<span style=
"color: #008080"
> 54</span>
<span style=
"color: #008080"
> 55</span> <span style=
"color: #000000"
> 6. 指定匹配位置:
</span><span style=
"color: #008080"
> 56</span> <span style=
"color: #000000"
> ^:开头位置的xxx,专用在表达式开头
</span><span style=
"color: #008080"
> 57</span> <span style=
"color: #000000"
> $:结尾位置的xxx,专用在表达式结尾
</span><span style=
"color: #008080"
> 58</span> <span style=
"color: #000000"
> 比如:^\s+ 匹配开始位置的所有空字符
</span><span style=
"color: #008080"
> 59</span> <span style=
"color: #000000"
> \s+$ 匹配结尾位置的所有空字符
</span><span style=
"color: #008080"
> 60</span> <span style=
"color: #000000"
> ^\s+|\s+$ 匹配开头或结尾的空字符
</span><span style=
"color: #008080"
> 61</span> <span style=
"color: #000000"
> 前加^后加$(不带|):表示从头到尾完整匹配
</span><span style=
"color: #008080"
> 62</span> <span style=
"color: #000000"
> 今后只要验证格式,都要前加^,后加$
</span><span style=
"color: #008080"
> 63</span> <span style=
"color: #000000"
> 比如:验证密码:6位数字:^\d{6}$
</span><span style=
"color: #008080"
> 64</span>
<span style=
"color: #008080"
> 65</span> <span style=
"color: #000000"
> \b:单词边界,每个单词前或后,必须有空字符分割——了解
</span><span style=
"color: #008080"
> 66</span>
<span style=
"color: #008080"
> 67</span> <span style=
"color: #000000"
> 预判:在正式匹配前,使用特定条件先预检查一次字符串
</span><span style=
"color: #008080"
> 68</span> <span style=
"color: #000000"
> 如果预判都未通过,则不再匹配
</span><span style=
"color: #008080"
> 69</span>
<span style=
"color: #008080"
> 70</span> <span style=
"color: #000000"
> 6位以上字母,数字的组合,至少包含一位大写字母和一位数字
</span><span style=
"color: #008080"
> 71</span> <span style=
"color: #000000"
> 1. 预判:不都由小写字母或数字组成
</span><span style=
"color: #008080"
> 72</span> <span style=
"color: #000000"
> 结论:可能包含大写字母,特殊符号
</span><span style=
"color: #008080"
> 73</span> <span style=
"color: #000000"
> (?![a-z0-9]+$)
</span><span style=
"color: #008080"
> 74</span> <span style=
"color: #000000"
> 2. 预判:不都由大写字母或小写字母组成
</span><span style=
"color: #008080"
> 75</span> <span style=
"color: #000000"
> 结论:可能包含数字,特殊符号
</span><span style=
"color: #008080"
> 76</span> <span style=
"color: #000000"
> (?![A-Za-z]+$)
</span><span style=
"color: #008080"
> 77</span> <span style=
"color: #000000"
> 3. 6位以上字母,数字的组合(排除特殊字符)
</span><span style=
"color: #008080"
> 78</span> <span style=
"color: #000000"
> 结论:肯定包含大写字母和数字
</span><span style=
"color: #008080"
> 79</span> <span style=
"color: #000000"
> [a-zA-Z0-9]{6,}
</span><span style=
"color: #008080"
> 80</span> <span style=
"color: #000000"
> (?![a-z0-9]+$)(?![A-Za-z]+$)[a-zA-Z0-9]{6,}
</span><span style=
"color: #008080"
> 81</span>
<span style=
"color: #008080"
> 82</span> <span style=
"color: #000000"
> 补充:汉字的备选字符集:[\u4e00-\u9fa5]
</span><span style=
"color: #008080"
> 83</span>
<span style=
"color: #008080"
> 84</span> <span style=
"color: #000000"
>2. String中支持正则的API:
</span><span style=
"color: #008080"
> 85</span> <span style=
"color: #000000"
> 1. 查找:4种:
</span><span style=
"color: #008080"
> 86</span> <span style=
"color: #000000"
> 1.
var
i=str.indexOf/lastIndexOf(
"敏感词"
[,fromi])
</span><span style=
"color: #008080"
> 87</span> <span style=
"color: #000000"
> 缺点:不支持正则表达式,每次只能找一个敏感词
</span><span style=
"color: #008080"
> 88</span> <span style=
"color: #000000"
> 优点:可指定开始查找的位置
</span><span style=
"color: #008080"
> 89</span> <span style=
"color: #000000"
> 何时使用:敏感词固定时,且仅获得位置时
</span><span style=
"color: #008080"
> 90</span> <span style=
"color: #000000"
> 2.
var
i=str.search(/正则表达式/)——不支持后缀g
</span><span style=
"color: #008080"
> 91</span> <span style=
"color: #000000"
> 缺:仅查找*第一个*和正则表达式匹配的关键词
</span><span style=
"color: #008080"
> 92</span> <span style=
"color: #000000"
> 仅返回关键词第一个字符的位置下标
</span><span style=
"color: #008080"
> 93</span> <span style=
"color: #000000"
> 如果没找到,返回-1.
</span><span style=
"color: #008080"
> 94</span> <span style=
"color: #000000"
> 无法返回关键词内容。
</span><span style=
"color: #008080"
> 95</span> <span style=
"color: #000000"
> 优:支持正则—
</span><span style=
"color: #008080"
> 96</span> <span style=
"color: #000000"
> 何时使用: 需要使用正则表达式查找关键字时,且只判断有没有。
</span><span style=
"color: #008080"
> 97</span> <span style=
"color: #000000"
> 3.
var
arr=str.match(/正则表达式/g)
</span><span style=
"color: #008080"
> 98</span> <span style=
"color: #000000"
> 缺:可返回所有匹配关键词的内容,
</span><span style=
"color: #008080"
> 99</span> <span style=
"color: #000000"
> 无法获得每个关键词的位置。
</span><span style=
"color: #008080"
>100</span> <span style=
"color: #000000"
> 优:找到所有关键词
</span><span style=
"color: #008080"
>101</span> <span style=
"color: #000000"
> 默认正则表达式只匹配一个关键词
</span><span style=
"color: #008080"
>102</span> <span style=
"color: #000000"
> 表达式后+g,让表达式匹配所有关键词
</span><span style=
"color: #008080"
>103</span> <span style=
"color: #000000"
> ***找不到,返回null!
</span><span style=
"color: #008080"
>104</span> <span style=
"color: #000000"
> 都要先判断不是null,再调用属性或方法
</span><span style=
"color: #008080"
>105</span> <span style=
"color: #000000"
> 4. 即可获得关键字内容,又可获得位置:reg.
exec
(待续)
</span><span style=
"color: #008080"
>106</span>
<span style=
"color: #008080"
>107</span> <span style=
"color: #000000"
> 2. 替换:
var
newStr=str.replace(/正则表达式/g,
"替换内容"
)
</span><span style=
"color: #008080"
>108</span> <span style=
"color: #000000"
> 删除:将匹配内容替换为
""
</span><span style=
"color: #008080"
>109</span> <span style=
"color: #000000"
> 比如:去掉字符串开头和结尾的空字符
</span><span style=
"color: #008080"
>110</span> <span style=
"color: #000000"
> 格式化:2步:
</span><span style=
"color: #008080"
>111</span> <span style=
"color: #000000"
> 1. 先定义正则表达式,用()分组
</span><span style=
"color: #008080"
>112</span> <span style=
"color: #000000"
> 每个(),都会得到一个序号:
$1
,
$2
,...
</span><span style=
"color: #008080"
>113</span> <span style=
"color: #000000"
> 2. 在replace中的替换内容部分,使用
$n
拼接结果
</span><span style=
"color: #008080"
>114</span> <span style=
"color: #000000"
> 匹配时,会自动将
$n
换成本次匹配的实际内容
</span><span style=
"color: #008080"
>115</span> <span style=
"color: #000000"
> 比如:
var
birth=
"19831226"
;
</span><span style=
"color: #008080"
>116</span> <span style=
"color: #000000"
> birth=birth.replace(
</span><span style=
"color: #008080"
>117</span> <span style=
"color: #000000"
> /(\d{4})(\d{2})(\d{2})/,
"$1年$2月$3日"
);
</span><span style=
"color: #008080"
>118</span> <span style=
"color: #000000"
> 3. 切割:
</span><span style=
"color: #008080"
>119</span> <span style=
"color: #000000"
>
var
arr=str.split(/分隔符正则表达式/);
</span><span style=
"color: #008080"
>120</span> <span style=
"color: #000000"
>3. ***RegExp对象:正则表达式对象
</span><span style=
"color: #008080"
>121</span> <span style=
"color: #000000"
> 封装了一个正则表达式,提供了操作正则表达式的方法
</span><span style=
"color: #008080"
>122</span>
<span style=
"color: #008080"
>123</span> <span style=
"color: #000000"
> 创建正则表达式:2种:
</span><span style=
"color: #008080"
>124</span> <span style=
"color: #000000"
> 1. 创建固定正则表达式对象:
</span><span style=
"color: #008080"
>125</span> <span style=
"color: #000000"
>
var
reg=/正则表达式/g;
</span><span style=
"color: #008080"
>126</span> <span style=
"color: #000000"
> 何时使用:如果正则表达式不需要动态生成时
</span><span style=
"color: #008080"
>127</span> <span style=
"color: #000000"
> 2. 创建可变正则表达式对象:
</span><span style=
"color: #008080"
>128</span> <span style=
"color: #000000"
>
var
reg=
new
RegExp(
"正则表达式"
,
"后缀"
);
</span><span style=
"color: #008080"
>129</span> <span style=
"color: #000000"
> 何时使用:如果正则表达式由变量或字符串动态生成时
</span><span style=
"color: #008080"
>130</span> <span style=
"color: #000000"
> 强调:所有\都要写成\\。比如:
"\d{6}"
-->
"\\{6}"
</span><span style=
"color: #008080"
>131</span>
<span style=
"color: #008080"
>132</span> <span style=
"color: #000000"
> 后缀:g:
global
全局/全部
</span><span style=
"color: #008080"
>133</span> i: 忽略大小写</span>