Search code examples
javascriptregexregex-group

Regex in Javascript extract and group set if characters between dates


I have the following list:

ID:26 09/21 17:03 13:47 13:14 06:57 09/22 18:01 13:47 13:13 07:00 09/23 17:46 14:12 13:35 07:17 09/24 16:15 13:17 12:45 07:08

I need to extract and group by dates like:

09/21 17:03 13:47 13:14 06:57
09/22 18:01 13:47 13:13 07:00
09/23 17:46 14:12 13:35 07:17
09/24 16:15 13:17 12:45 07:08

I've tried using (\d+[/]\d+()) but it only extracts the dates like:

09/21
09/22
09/23
09/24

and then tried grouping by using (\/[^:]+?.+?)\/ with the following results

/21 17:03 13:47 13:14 06:57 09/
/23 17:46 14:12 13:35 07:17 09/

where it's missing the middle groups

I'm just strating with regex and any heklp will be highly appreciatted!


Solution

  • try this one.

    \d{2}\/\d{2} -> this will match the date

    (\s\d{2}:\d{2})+ -> this will match the following times separated by space

    var str = 'ID:26 09/21 17:03 13:47 13:14 06:57 09/22 18:01 13:47 13:13 07:00 09/23 17:46 14:12 13:35 07:17 09/24 16:15 13:17 12:45 07:08';
    var matches = str.match(/\d{2}\/\d{2}(\s\d{2}:\d{2})+/g);
    console.log(matches)