javascript - 正则表达式删除具有已知开始和结束模式的子字符串,中间有多个单词
问题描述
这是我要删除的示例子字符串:
2018 年 2 月 4 日晚上 11:00,
问题是它可以在一个字符串中出现多次。我只想删除它/替换任何内容,例如。''
.
你可以用一种懒惰的方式来做到这一点,你知道模式的长度,将其子串化。
我试过这样的正则表达式:
str.replaceAll(/^On*PM,$/g, '')
^
和表示开始结束的地方$
......我错过了空格/多个单词。
月份/时间是动态的,但没有那么多组合。
解决方案
利用
/\bOn\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept?|Oct|Nov|Dec)\s+(?:0?[1-9]|[12]\d|3[01]),\s*\d{4}\s*(?:0?[0-9]|1[0-2]):[0-5]?[0-9]\s*[AP]M,/gi
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
On 'On'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
Jan 'Jan'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Feb 'Feb'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Mar 'Mar'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Apr 'Apr'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
May 'May'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jun 'Jun'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jul 'Jul'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Aug 'Aug'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Sep 'Sep'
--------------------------------------------------------------------------------
t? 't' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Oct 'Oct'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Nov 'Nov'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Dec 'Dec'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[12] any character of: '1', '2'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
3 '3'
--------------------------------------------------------------------------------
[01] any character of: '0', '1'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-2] any character of: '0' to '2'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
[0-5]? any character of: '0' to '5' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[AP] any character of: 'A', 'P'
--------------------------------------------------------------------------------
M, 'M,'
或者,如果您处理的字符串状态良好,则更简单:
/\bOn\s+\w+\s+\d{1,2},\s*\d{4}\s*\d{1,2}:\d{1,2}\s*[AP]M,/gi
见这个证明。
解释
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
On 'On'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[AP] any character of: 'A', 'P'
--------------------------------------------------------------------------------
M, 'M,'
推荐阅读
- linux - Simd 不在我的 Linux 机器上:致命错误:simd/simd.h:没有这样的文件或目录
- python - jinja + 表单 + unicode 控制字符 + xml/docx 集成
- r - 使用 dplyr 和 mutate 结转损失
- swift - 使用没有 nib 的 NSTableView 创建 NSScrollView — TableView 上缺少约束
- c# - 通过 Linq 和 EF 选择链接表的空列表
- android - Android studio error - react native using firebase
- greenplum - 表greenplum数据库中缺少分布键
- excel - 如何从 maatwebsite/Excel 向集合添加额外数据
- git - 如何获取 github 文件的查询 url?
- python - 在尝试粘贴时,使用 tkinter 从 Windows 10 中的 Python 3.4 复制到剪贴板会导致崩溃