Ando Ando - 6 months ago 11
Javascript Question

regex group results and or

Is it possible to return an array with two of the important regex checks?

Something like this -

/(?=.*\d days)(?=.*car|truck|motorcycle)/


First problem is that the regex doesn't match car or other vehicles...
Second - is it possible to extract the number of days
...*\d days(...
into a separate array value?

At the moment my regex is -
/(?=.*\d days)(?=.*car)/

and the response is -
[ '', index: 0, input: '5 days before cleaning the car' ]

and i'm striving for -
[ '5', index: 0, input: '5 days before cleaning the car']


Inputs


  1. 5 days before cleaning a car

  2. Motorcycle cleaning in 23 days

  3. truck will arrive in 10 days



Basically i want to get the number of days for any of the vehicles that might exist in a short text.

Answer

Description

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

Regular expression visualization

This regex will do the following:

  • capture the number of days
  • capture the type of vehicle

Examples

Live Demo

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

Sample Text

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

Sample Matches

[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

Explanation

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))
----------------------------------------------------------------------