File tree Expand file tree Collapse file tree 7 files changed +121
-128
lines changed
9-regular-expressions/10-regexp-greedy-and-lazy
4-find-html-tags-greedy-lazy Expand file tree Collapse file tree 7 files changed +121
-128
lines changed Original file line number Diff line number Diff line change 11
22结果是:` match:123 4 ` 。
33
4- 首先,懒惰模式 ` pattern:\d+? ` 尝试去获取尽可能少的字符,但当它检测到空格,就得出匹配结果 ` match:123 ` 。
4+ 首先,惰性模式 ` pattern:\d+? ` 尝试去获取尽可能少的数字,但它必须到达空格,因此需要匹配到 ` match:123 ` 。
55
6- 然后,第二个 ` \d+? ` 就只获取一个字符,因为这就已足够了 。
6+ 然后,第二个 ` \d+? ` 就只获取一个数字,因为这就已经足够了 。
Original file line number Diff line number Diff line change 1- # 对于 /d+? d+?/ 的匹配
1+ # /d+? d+?/ 的匹配项
22
3- 以下匹配的结果是什么 ?
3+ 匹配的结果是什么 ?
44
55``` js
66alert ( " 123 456" .match (/ \d +? \d +? / g ) ); // ?
Original file line number Diff line number Diff line change 11我们需要找到注释的起始位置 ` match:<!-- ` ,然后获取字符直到注释的末尾 ` match:--> ` 。
22
3- 首先想到的是 ` pattern:<!--.*?--> ` —— 惰性量词使得点(.)停在 ` match:--> ` 之前。
3+ 行得通的表达式可以是 ` pattern:<!--.*?--> ` —— 惰性量词使得点在 ` match:--> ` 之前 停止。我们还需要为点添加修饰符 ` pattern:s ` 以包含换行符 。
44
5- 但是在 Javascript 中,一个点(.)表示除换行符之外的任意字符。所以这是无法匹配多行注释的。
6-
7- 我们可以用 ` pattern:[\s\S] ` ,而不是用点(.)来匹配“任何东西”:
5+ 否则找不到多行注释:
86
97``` js run
10- let reg = / <!--[ \s\S ] *? -->/ g ;
8+ let regexp = / <!--. *? -->/ gs ;
119
1210let str = ` ... <!-- My -- comment
1311 test --> .. <!----> ..
1412` ;
1513
16- alert ( str .match (reg ) ); // '<!-- My -- comment \n test -->', '<!---->'
14+ alert ( str .match (regexp ) ); // '<!-- My -- comment \n test -->', '<!---->'
1715```
Original file line number Diff line number Diff line change 11# 查找 HTML 注释
22
3- 找出文本中的所有注释 :
3+ 找出文本中的所有 HTML 注释 :
44
55``` js
6- let reg = / 你的正则表达式/ g ;
6+ let regexp = / 你的正则表达式/ g ;
77
88let str = ` ... <!-- My -- comment
99 test --> .. <!----> ..
1010` ;
1111
12- alert ( str .match (reg ) ); // '<!-- My -- comment \n test -->', '<!---->'
12+ alert ( str .match (regexp ) ); // '<!-- My -- comment \n test -->', '<!---->'
1313```
Original file line number Diff line number Diff line change 22答案是 ` pattern:<[^<>]+> ` 。
33
44``` js run
5- let reg = / <[^ <>] + >/ g ;
5+ let regexp = / <[^ <>] + >/ g ;
66
77let str = ' <> <a href="/"> <input type="radio" checked> <b>' ;
88
9- alert ( str .match (reg ) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
9+ alert ( str .match (regexp ) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
1010```
Original file line number Diff line number Diff line change 11# 寻找 HTML 标签
22
3- 创建一个正则表达式语句来寻找所有具有其属性的(闭合或非闭合 )HTML 标签 。
3+ 创建一个正则表达式来寻找所有(开始和结束 )HTML 标签及其特性 。
44
55用例:
66
77``` js run
8- let reg = / 你的正则表达式/ g ;
8+ let regexp = / 你的正则表达式/ g ;
99
1010let str = ' <> <a href="/"> <input type="radio" checked> <b>' ;
1111
12- alert ( str .match (reg ) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
12+ alert ( str .match (regexp ) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
1313```
1414
15- 假设标签属性中不会包含 ` < ` 和 ` > ` (包括被引号包裹的内容),这样就简单许多 。
15+ 这里我们假设标签特征中不包含 ` < ` 和 ` > ` (包括被引号包裹的内容),这样就简单多了 。
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments