4

I have an an array containing various timeslots :

var timeArray = ["00:05 - 02:50", "03:05 - 05:50", "05:10 - 07:55", "06:25 - 09:10", "07:55 - 10:40", "09:00 - 11:45", "15:10 - 17:55", "17:05 - 19:45", "18:50 - 21:35", "19:40 - 22:25", "20:45 - 23:40", "22:00 - 00:45", "22:40 - 01:25", "11:55 - 16:15"]

I want to search all the timeslots between 06-12 and push them in an array. I wrote below js but this is returning me an empty array:

var resultArray = new Array();
for (var i = 0; i < timeArray.length; i++) {
    var bar = /^06:^07:^08:^09:^10:^11/;
    if (bar.test(timeArray[i])) {
        alert("found desired timeslots");
        resultArray.push(timeArray[i]);
    }
};
5
  • Did you mean to match the first two digits? /^(?:0[6-9]|1[01])/? Commented Jul 13, 2018 at 6:43
  • Basically I need to fix the regex variable Commented Jul 13, 2018 at 6:44
  • 1
    Change the : to a | Commented Jul 13, 2018 at 6:44
  • FWIW, ^06:^07:^08:^09:^10:^11 matches a string starting with "06:^07:^08:^09:^10:^11" (like literally); regular expressions follow rules, use a guide/reference and follow guided practice to increase productivity - because there an infinite set of invalid patterns :} Commented Jul 13, 2018 at 6:45
  • 1
    Hi @WiktorStribiżew that worked . Thank you. Commented Jul 13, 2018 at 6:49

2 Answers 2

3

You could takes some groups for the wanted times and use only one start indicator.

var timeArray = ["00:05 - 02:50", "03:05 - 05:50", "05:10 - 07:55", "06:25 - 09:10", "07:55 - 10:40", "09:00 - 11:45", "15:10 - 17:55", "17:05 - 19:45", "18:50 - 21:35", "19:40 - 22:25", "20:45 - 23:40", "22:00 - 00:45", "22:40 - 01:25", "11:55 - 16:15"],
    resultArray = timeArray.filter(s => /^(0[6-9]|1(0|1))/.test(s));

console.log(resultArray);

Sign up to request clarification or add additional context in comments.

Comments

1

The regular expression should rather be: ^06|^07|^08|^09|^10|^11. Another more concise alternative would be: /^0[6-9]|^1[01]/

Runnable example below:

var timeArray = ["00:05 - 02:50", "03:05 - 05:50", "05:10 - 07:55", "06:25 - 09:10", "07:55 - 10:40", "09:00 - 11:45", "15:10 - 17:55", "17:05 - 19:45", "18:50 - 21:35", "19:40 - 22:25", "20:45 - 23:40", "22:00 - 00:45", "22:40 - 01:25", "11:55 - 16:15"]

var resultArray = new Array();
for (var i = 0; i < timeArray.length; i++) {
  var bar = /^06|^07|^08|^09|^10|^11/;
  if (bar.test(timeArray[i])) {
    console.log("found desired timeslots: " + timeArray[i]);
    resultArray.push(timeArray[i]);
  }
};

2 Comments

If you repeat the start of string anchor, you do not need a grouping construct here. var bar = /^06|^07|^08|^09|^10|^11/; will do.
@WiktorStribiżew yes, you are right. I have updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.