Regular/排除:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第1行: 第1行:
== 排除 ==
== 排除 ==
=== 过滤 ===
== 过滤 ==
==== 过滤目标内容 ====
==== 过滤目标内容 ====
在正则匹配过程中过滤掉 <code>\d</code> 中的数字 <code>2</code>。
在正则匹配过程中过滤掉 <code>\d</code> 中的数字 <code>2</code>。
第36行: 第36行:
// (8) ['3', '4', '5', '6', '7', '8', '9', '0']
// (8) ['3', '4', '5', '6', '7', '8', '9', '0']
</syntaxhighlight>
</syntaxhighlight>
|-
| <pre>
^(?:(?!kangkk)(?![#_])\w)+$</pre> || <html><svg width="355.4375" height="80" style="margin: 60px 0px; zoom: 1;"><g transform="translate(37, 48)" style="display: inline;"><g><g><g><g transform="translate(4, 0)"><rect x="0" y="-11" rx="2" ry="2" width="15.203125" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">^</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M19.203125,0H15.203125"></path></g><g transform="translate(31.203125, 0)"><g><g><g transform="translate(28, 0)"><rect x="-24" y="-19" width="113.21875" height="38" rx="5" ry="5" class="nfa-lookahead"></rect><g><rect x="0" y="-11" rx="2" ry="2" width="71.21875" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan dx="0" style="letter-spacing: 0px;">k</tspan><tspan dx="4" style="letter-spacing: 0px;">a</tspan><tspan dx="4" style="letter-spacing: 0px;">n</tspan><tspan dx="4" style="letter-spacing: 0px;">g</tspan><tspan dx="4" style="letter-spacing: 0px;">k</tspan><tspan dx="4" style="letter-spacing: 0px;">k</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M75.21875,0H71.21875"></path></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M79.21875,0H75.21875"></path><circle class="nfa-start" cx="-11" cy="0" r="3"></circle><circle class="nfa-end" cx="82.21875" cy="0" r="3"></circle><path class="nfa-path" d="M-14,0L-20,0M-14,0L-16.4,-3.2M-14,0L-16.4,3.2"></path><text class="nfa-label" x="32.609375" y="-21">if not followed by</text><path class="nfa-path" d="M-28,0H-24"></path><path class="nfa-path" d="M93.21875,0H89.21875"></path></g><g transform="translate(149.21875, 0)"><rect x="-24" y="-31" width="71.40625" height="50" rx="5" ry="5" class="nfa-lookahead"></rect><g><rect x="0" y="-11" rx="2" ry="2" width="29.40625" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan dx="0" style="letter-spacing: 0px;">#</tspan><tspan dx="7" style="letter-spacing: 0px;">_</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M33.40625,0H29.40625"></path><text class="nfa-label" x="14.703125" y="-13">One of</text></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M37.40625,0H33.40625"></path><circle class="nfa-start" cx="-11" cy="0" r="3"></circle><circle class="nfa-end" cx="40.40625" cy="0" r="3"></circle><path class="nfa-path" d="M-14,0L-20,0M-14,0L-16.4,-3.2M-14,0L-16.4,3.2"></path><text class="nfa-label" x="11.703125" y="-33">if not followed by</text><path class="nfa-path" d="M-28,0H-24"></path><path class="nfa-path" d="M51.40625,0H47.40625"></path></g><g transform="translate(204.625, 0)"><rect x="0" y="-11" rx="2" ry="2" width="22.40625" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">\w</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M26.40625,0H22.40625"></path></g></g></g><path class="nfa-path" d="M-8,0H0"></path><path class="nfa-path" d="M239.03125,0H231.03125"></path><path class="nfa-path" d="M227.03125,0Q235.03125,0,235.03125,8V19Q235.03125,27,227.03125,27H4Q-4,27,-4,19V8Q-4,0,4,0"></path></g><g transform="translate(274.234375, 0)"><rect x="0" y="-11" rx="2" ry="2" width="15.203125" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">$</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M19.203125,0H15.203125"></path></g></g></g><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M297.4375,0H293.4375"></path><circle class="nfa-start" cx="-12" cy="0" r="8"></circle><circle class="nfa-end" cx="305.4375" cy="0" r="8"></circle><path class="nfa-path" d="M-20,0L-32,0M-20,0L-24.8,-3.2M-20,0L-24.8,3.2"></path><circle class="nfa-marker sa-blue" cx="305.4375" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || ||
|}
|}


第52行: 第49行:
// (2) ['abcd', 'efg_hi']
// (2) ['abcd', 'efg_hi']
</syntaxhighlight>
</syntaxhighlight>
|-
| <pre>
^(?:(?!kangkk)(?![#_])\w)+$</pre> || <html><svg width="355.4375" height="80" style="margin: 60px 0px; zoom: 1;"><g transform="translate(37, 48)" style="display: inline;"><g><g><g><g transform="translate(4, 0)"><rect x="0" y="-11" rx="2" ry="2" width="15.203125" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">^</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M19.203125,0H15.203125"></path></g><g transform="translate(31.203125, 0)"><g><g><g transform="translate(28, 0)"><rect x="-24" y="-19" width="113.21875" height="38" rx="5" ry="5" class="nfa-lookahead"></rect><g><rect x="0" y="-11" rx="2" ry="2" width="71.21875" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan dx="0" style="letter-spacing: 0px;">k</tspan><tspan dx="4" style="letter-spacing: 0px;">a</tspan><tspan dx="4" style="letter-spacing: 0px;">n</tspan><tspan dx="4" style="letter-spacing: 0px;">g</tspan><tspan dx="4" style="letter-spacing: 0px;">k</tspan><tspan dx="4" style="letter-spacing: 0px;">k</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M75.21875,0H71.21875"></path></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M79.21875,0H75.21875"></path><circle class="nfa-start" cx="-11" cy="0" r="3"></circle><circle class="nfa-end" cx="82.21875" cy="0" r="3"></circle><path class="nfa-path" d="M-14,0L-20,0M-14,0L-16.4,-3.2M-14,0L-16.4,3.2"></path><text class="nfa-label" x="32.609375" y="-21">if not followed by</text><path class="nfa-path" d="M-28,0H-24"></path><path class="nfa-path" d="M93.21875,0H89.21875"></path></g><g transform="translate(149.21875, 0)"><rect x="-24" y="-31" width="71.40625" height="50" rx="5" ry="5" class="nfa-lookahead"></rect><g><rect x="0" y="-11" rx="2" ry="2" width="29.40625" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan dx="0" style="letter-spacing: 0px;">#</tspan><tspan dx="7" style="letter-spacing: 0px;">_</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M33.40625,0H29.40625"></path><text class="nfa-label" x="14.703125" y="-13">One of</text></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M37.40625,0H33.40625"></path><circle class="nfa-start" cx="-11" cy="0" r="3"></circle><circle class="nfa-end" cx="40.40625" cy="0" r="3"></circle><path class="nfa-path" d="M-14,0L-20,0M-14,0L-16.4,-3.2M-14,0L-16.4,3.2"></path><text class="nfa-label" x="11.703125" y="-33">if not followed by</text><path class="nfa-path" d="M-28,0H-24"></path><path class="nfa-path" d="M51.40625,0H47.40625"></path></g><g transform="translate(204.625, 0)"><rect x="0" y="-11" rx="2" ry="2" width="22.40625" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">\w</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M26.40625,0H22.40625"></path></g></g></g><path class="nfa-path" d="M-8,0H0"></path><path class="nfa-path" d="M239.03125,0H231.03125"></path><path class="nfa-path" d="M227.03125,0Q235.03125,0,235.03125,8V19Q235.03125,27,227.03125,27H4Q-4,27,-4,19V8Q-4,0,4,0"></path></g><g transform="translate(274.234375, 0)"><rect x="0" y="-11" rx="2" ry="2" width="15.203125" height="22" class="nfa-literal"></rect><text transform="translate(4, 4)" class="nfa-literal-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">$</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M19.203125,0H15.203125"></path></g></g></g><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M297.4375,0H293.4375"></path><circle class="nfa-start" cx="-12" cy="0" r="8"></circle><circle class="nfa-end" cx="305.4375" cy="0" r="8"></circle><path class="nfa-path" d="M-20,0L-32,0M-20,0L-24.8,-3.2M-20,0L-24.8,3.2"></path><circle class="nfa-marker sa-blue" cx="305.4375" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || ||
|}
|}

2022年11月9日 (三) 04:17的版本

排除

过滤

过滤目标内容

在正则匹配过程中过滤掉 \d 中的数字 2。

表达式 图例 测试文本 示例
(?=\d)[^2]
\dif followed by2None of 1234567890
const foo = '1234567890';
foo.match(/(?=\d)[^2]/ig);
// (9) ['1', '3', '4', '5', '6', '7', '8', '9', '0']
(?!2)\d
2if not followed by\d 同上 同上
[0-13-9]
0-13-9One of 同上 同上

过滤目标前面内容

在正则匹配过程中过滤掉 \d 中的数字 2 之前的内容。

表达式 图例 测试文本 示例
(?!.*?2)\d+
.2if not followed by\d 1234567890
const foo = '1234567890';
foo.match(/(?!.*?2)\d+/ig);
// ['34567890']
foo.match(/(?!.*?2)\d/ig);
// (8) ['3', '4', '5', '6', '7', '8', '9', '0']

其他

表达式 图例 测试文本 示例
\w+(?!#|_|f)
\w#_fif not followed by abcd#efg_hi
const bar = 'abcd#efg_hi';
bar.match(/\w+(?<!#|_|f)/ig)
// (2) ['abcd', 'efg_hi']
^(?:(?!kangkk)(?![#_])\w)+$
^kangkkif not followed by#_One ofif not followed by\w$