正则表达式匹配所有内容直到删除尾随空格

时间:2017-09-26 18:56:09

标签: javascript regex

我有一个像这样的字符串

IF condition EQUALS value THEN do this

我希望得到没有任何开头或尾随空格的条件。这就是我试过的:

/\b(IF)\b[\w\d\s]+?(?=EQUALS)(EQUALS)/g

除了条件之前和之后都有空格时,几乎可以正常工作。

请在此处查看完整示例:https://regex101.com/r/qsjlia/4

2 个答案:

答案 0 :(得分:1)

您可以尝试以下正则表达式:

(?:^IF\s+)(\w+)(?=\s+EQUALS\s)

DEMO

答案 1 :(得分:1)

在这个提议的解决方案中,我已经假设在某些时候你想要条件等于条款,以及然后&的否则

描述

这个正则表达式

IF\s+((?:(?!\s(?:NOT)?EQUALS\s).)*)\s+((?:NOT)?EQUALS)\s+((?:(?!\sTHEN\s).)*)\s+THEN\s+((?:(?!\sELSE\s).)*)\s+ELSE\s+(.*)

enter image description here

**要更好地查看图像,只需右键单击图像并在新窗口中选择视图

将执行以下操作:

  • 拉出条件,等同条款,值,然后和其他短语
  • 将捕获的字符存储到编号的捕获组中。
  • 在每个文本块之前和之后忽略空格

实施例

另见live demo

给出您的示例文本

IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28 
IF factor EQUALS value THEN do this ELSE do something else. 
IF factor NOTEQUALS value THEN do this ELSE do something else. 
IF 2 NOTEQUALS 'Droids' THEN Not the droids you are looking for ELSE Found the droids

正则表达式将创建以下捕获组:

  • 捕获组0将获得整行
  • 捕获组1将获得Condition
  • 捕获组2将获得EQUALSNOTEQUALS
  • 捕获组3将获得Value
  • 捕获组4将获得Then
  • 捕获组5将获得Else

使用以下匹配项:

[0][0] = IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28
[0][1] = 45
[0][2] = EQUALS
[0][3] = 'Yes'
[0][4] = ASSIGN 28
[0][5] = SHOW 28

[1][0] = IF factor EQUALS value THEN do this ELSE do something else.
[1][1] = factor
[1][2] = EQUALS
[1][3] = value
[1][4] = do this
[1][5] = do something else.

[2][0] = IF factor NOTEQUALS value THEN do this ELSE do something else.
[2][1] = factor
[2][2] = NOTEQUALS
[2][3] = value
[2][4] = do this
[2][5] = do something else.

[3][0] = IF 2 NOTEQUALS 'Droids' THEN Found the droids ELSE not the droids you are looking for 
[3][1] = 2
[3][2] = NOTEQUALS
[3][3] = 'Droids'
[3][4] = Not the droids you are looking for 
[3][5] = Found the droids

简短说明

每个捕获组使用此高级Regex构造进行捕获,直到遇到已知的字符块。

从上面的表达式中,其中一个结构是((?:(?!\sTHEN\s).)*)\s+THEN\s+

NODE                     EXPLANATION
(                        group and capture 
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
        THEN                     'THEN'
----------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
  )                        end of capture group
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  THEN                     'THEN'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))

详细说明

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  IF                       'IF'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount
                                 possible)):
--------------------------------------------------------------------------------
          NOT                      'NOT'
--------------------------------------------------------------------------------
        )?                       end of grouping
--------------------------------------------------------------------------------
        EQUALS                   'EQUALS'
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      NOT                      'NOT'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    EQUALS                   'EQUALS'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
        THEN                     'THEN'
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  THEN                     'THEN'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
        ELSE                     'ELSE'
--------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \4
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  ELSE                     'ELSE'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \5:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \5