正则表达集团的结果和或

时间:2016-05-16 04:33:30

标签: javascript regex

是否可以返回包含两个重要正则表达式检查的数组?

这样的事情 - /(?=.*\d days)(?=.*car|truck|motorcycle)/

第一个问题是正则表达式与汽车或其他车辆不匹配...... 第二 - 是否可以将天数...*\d days(...提取到单独的数组值中?

目前我的正则表达式是 - /(?=.*\d days)(?=.*car)/ 响应是 - [ '', index: 0, input: '5 days before cleaning the car' ] 我正努力争取 - [ '5', index: 0, input: '5 days before cleaning the car']

输入

5 days before cleaning a car 
Motorcycle cleaning in 23 days 
truck will arrive in 10 days

基本上我想知道短文中可能存在的任何车辆的天数。

1 个答案:

答案 0 :(得分:3)

描述

^(?=.*?([0-9]+)\s*days)(?=.*?(motorcycle|truck|car)).*

Regular expression visualization

此正则表达式将执行以下操作:

  • 捕获天数
  • 捕捉车辆类型

实施例

现场演示

https://regex101.com/r/qY5nL6/1

示例文字

5 days before cleaning a car
Motorcycle cleaning in 23 days
truck will arrive in 10 days

样本匹配

[0][0] = 5 days before cleaning a car
[0][1] = 5
[0][2] = car

[1][0] = Motorcycle cleaning in 23 days
[1][1] = 23
[1][2] = Motorcycle

[2][0] = truck will arrive in 10 days
[2][1] = 10
[2][2] = truck

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      [0-9]+                   any character of: '0' to '9' (1 or
                               more times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    days                     'days'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      motorcycle               'motorcycle'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      truck                    'truck'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      car                      'car'
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------