Skip to content
Advertisement

How to get list of restaurant operating days with time in a week from the API response string [closed]

Getting the response from API as below

JavaScript

The output list should be like below

JavaScript

Advertisement

Answer

Assuming operatingHours is always going to be in one of the formats given below:

  1. Mon-Thu, Sun 11:30 am - 10:00 pm / Fri-Sat 11:30 am - 10:30 pm
  2. Mon-Sat 11:00 am - 11:00 pm / Sun 11:00 am - 10:30 pm

given below can be an alternative solution:

JavaScript

Output:

JavaScript

Here is the demo of the regex used to find the time range.

Explanation of the regex:

  1. d+ matches one or more digit(s)

  2. : matches the character : literally

  3. (?i) specifies case-insensitive match

  4. Non-capturing group (?:[ap]m)

    • [ap] matches a single character out of a and p
    • m matches the character m literally
  5. - matches the characters - literally

Advertisement