Regular/排除:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的1个中间版本)
第1行: 第1行:
== 标题文字 ==
{| class="wikitable"
|-
! 标题文字 !! 标题文字 !! 标题文字
|-
| <nowiki>\D</nowiki> || 排除数字 || 示例
|-
| <nowiki>\S</nowiki> || 排除空格 || 示例
|-
| <nowiki>[^\u4E00-\u9FA5]</nowiki> || 排除汉字 || 示例
|}
== 排除 ==
== 排除 ==
== 过滤 ==
== 过滤 ==
==== 过滤目标内容 ====
==== 过滤目标内容 ====
第19行: 第31行:
| <pre>
| <pre>
[0-13-9]</pre> || <html><svg class="nfasfg" width="127.8125" height="44" style="margin: 78px 0px; zoom: 1;"><g transform="translate(41, 28)" style="display: inline;"><g><g><g><rect x="0" y="-11" rx="2" ry="2" width="57.8125" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan dx="0" style="letter-spacing: 0px;">0</tspan><tspan class="nfa-charset-range">-</tspan><tspan dx="0" style="letter-spacing: 0px;">1</tspan><tspan dx="7" style="letter-spacing: 0px;">3</tspan><tspan class="nfa-charset-range">-</tspan><tspan dx="0" style="letter-spacing: 0px;">9</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M61.8125,0H57.8125"></path><text class="nfa-label" x="28.90625" y="-13">One of</text></g></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M65.8125,0H61.8125"></path><circle class="nfa-start" cx="-16" cy="0" r="8"></circle><circle class="nfa-end" cx="73.8125" cy="0" r="8"></circle><path class="nfa-path" d="M-24,0L-36,0M-24,0L-28.8,-3.2M-24,0L-28.8,3.2"></path><circle class="nfa-marker sa-blue" cx="73.8125" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || 同上 || 同上
[0-13-9]</pre> || <html><svg class="nfasfg" width="127.8125" height="44" style="margin: 78px 0px; zoom: 1;"><g transform="translate(41, 28)" style="display: inline;"><g><g><g><rect x="0" y="-11" rx="2" ry="2" width="57.8125" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan dx="0" style="letter-spacing: 0px;">0</tspan><tspan class="nfa-charset-range">-</tspan><tspan dx="0" style="letter-spacing: 0px;">1</tspan><tspan dx="7" style="letter-spacing: 0px;">3</tspan><tspan class="nfa-charset-range">-</tspan><tspan dx="0" style="letter-spacing: 0px;">9</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M61.8125,0H57.8125"></path><text class="nfa-label" x="28.90625" y="-13">One of</text></g></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M65.8125,0H61.8125"></path><circle class="nfa-start" cx="-16" cy="0" r="8"></circle><circle class="nfa-end" cx="73.8125" cy="0" r="8"></circle><path class="nfa-path" d="M-24,0L-36,0M-24,0L-28.8,-3.2M-24,0L-28.8,3.2"></path><circle class="nfa-marker sa-blue" cx="73.8125" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || 同上 || 同上
|}
在正则匹配过程中过滤掉匹配的内容。
{| class="wikitable"
|-
! 表达式 !! 图例 !! 测试文本 !! 示例
|-
| <pre>
[^\d]</pre> || <html><svg width="92.40625" height="44" style="margin: 78px 0px; zoom: 1;"><g transform="translate(41, 28)" style="display: inline;"><g><g><g><rect x="0" y="-11" rx="2" ry="2" width="22.40625" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">\d</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M26.40625,0H22.40625"></path><text class="nfa-label" x="11.203125" y="-13">None of</text></g></g><path class="nfa-path" d="M-8,0H-4"></path><path class="nfa-path" d="M30.40625,0H26.40625"></path><circle class="nfa-start" cx="-16" cy="0" r="8"></circle><circle class="nfa-end" cx="38.40625" cy="0" r="8"></circle><path class="nfa-path" d="M-24,0L-36,0M-24,0L-28.8,-3.2M-24,0L-28.8,3.2"></path><circle class="nfa-marker sa-blue" cx="38.40625" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || <code>a</code>1<code>bb</code>22<code>ccc</code>333 || <syntaxhighlight lang="javascript">
const foo = 'a1bb22ccc333';
foo.match(/[^\d]/ig)
// (6) ['a', 'b', 'b', 'c', 'c', 'c']</syntaxhighlight>
|-
| <pre>
[^\d]*</pre> ||<html><svg width="124.40625" height="60" style="margin: 70px 0px; zoom: 1;"><g transform="translate(57, 36)" style="display: inline;"><g><g><g><g><rect x="0" y="-11" rx="2" ry="2" width="22.40625" height="22" class="nfa-charset"></rect><text transform="translate(4, 4)" class="nfa-charset-text"><tspan class="nfa-escaped" dx="0" style="letter-spacing: 0px;">\d</tspan></text><path class="nfa-path" d="M-4,0H0"></path><path class="nfa-path" d="M26.40625,0H22.40625"></path><text class="nfa-label" x="11.203125" y="-13">None of</text></g><path class="nfa-path" d="M-12,0H-4"></path><path class="nfa-path" d="M34.40625,0H26.40625"></path><path class="nfa-path" d="M22.40625,0Q30.40625,0,30.40625,8V11Q30.40625,19,22.40625,19H0Q-8,19,-8,11V8Q-8,0,0,0"></path><path class="nfa-path" d="M-20,0H-12"></path><path class="nfa-path" d="M42.40625,0H34.40625"></path><path class="nfa-path" d="M42.40625,0Q34.40625,0,34.40625,-8V-23Q34.40625,-31,26.40625,-31H-4Q-12,-31,-12,-23V-8Q-12,0,-20,0"></path></g></g><path class="nfa-path" d="M-24,0H-20"></path><path class="nfa-path" d="M46.40625,0H42.40625"></path><circle class="nfa-start" cx="-32" cy="0" r="8"></circle><circle class="nfa-end" cx="54.40625" cy="0" r="8"></circle><path class="nfa-path" d="M-40,0L-52,0M-40,0L-44.8,-3.2M-40,0L-44.8,3.2"></path><circle class="nfa-marker sa-blue" cx="54.40625" cy="0" r="5"></circle></g></g><g class="backref-container"></g></svg></html> || 同上 || <syntaxhighlight lang="javascript">
const foo = 'a1bb22ccc333';
foo.match(/[^\d]*/ig)
// (10) ['a', '', 'bb', '', '', 'ccc', '', '', '', '']</syntaxhighlight>
|-
| 示例 || 示例 || 示例 || 示例
|}
|}



2022年11月9日 (三) 05:58的最新版本

标题文字

标题文字 标题文字 标题文字
\D 排除数字 示例
\S 排除空格 示例
[^\u4E00-\u9FA5] 排除汉字 示例

排除

过滤

过滤目标内容

在正则匹配过程中过滤掉 \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]
\dNone of a1bb22ccc333
const foo = 'a1bb22ccc333';
foo.match(/[^\d]/ig)
// (6) ['a', 'b', 'b', 'c', 'c', 'c']
[^\d]*
\dNone of 同上
const foo = 'a1bb22ccc333';
foo.match(/[^\d]*/ig)
// (10) ['a', '', 'bb', '', '', 'ccc', '', '', '', '']
示例 示例 示例 示例

过滤目标前面内容

在正则匹配过程中过滤掉 \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$