首页 > 解决方案 > 正则表达式删除具有已知开始和结束模式的子字符串,中间有多个单词

问题描述

这是我要删除的示例子字符串:

2018 年 2 月 4 日晚上 11:00,

问题是它可以在一个字符串中出现多次。我只想删除它/替换任何内容,例如。''.

你可以用一种懒惰的方式来做到这一点,你知道模式的长度,将其子串化。

我试过这样的正则表达式:

str.replaceAll(/^On*PM,$/g, '')

^和表示开始结束的地方$......我错过了空格/多个单词。

月份/时间是动态的,但没有那么多组合。

标签: javascriptregex

解决方案


利用

/\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,'

推荐阅读