-2

i have string that looks like this

"sunday# 00:01 - 23:59?00:01 - 23:59?00:01 - 23:59?00:01 - 23:59"

its activity hours per day

how do i extract each formatHH:MM - HH:MM and put all of them into array? so ill have ["23-59 - 00:01","23-59 - 00:01","23-59 - 00:01","23-59 - 00:01"]

i might receive "sunday# close" or "sunday# 00:01 - 23:59"

i want to extract all activity hours from that string into array cause once i have it in array i print it as table in gui

Sloth
  • 29
  • 6
  • 1
    A little more explanation is needed here. What do these sub-strings represent, and what is the general format? The example given is a little too ambiguous, and you haven't provided enough context. – Musab Guma'a Jul 08 '20 at 17:35

2 Answers2

2

I believe this should work for you well !

var str = "sunday# 00:01 - 23:59?00:01 - 23:59?00:01 - 23:59?00:01 - 23:59";

var result = [...str.matchAll(/[0-2]{1}[0-3]{1}:[0-5]{1}[0-9]{1} - [0-2]{1}[0-3]{1}:[0-5]{1}[0-9]{1}/g)];

console.log(result.flat());
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

Using just Array.split and Array.map should do the job for what you want:

    var str = "sunday# 00:01 - 23:59?00:01 - 23:59?00:01 - 23:59?00:01 - 23:59";
var result = str.split("#")[1].trim().split("?");
if(result.length > 1){
    result.map(e=>{var s = e.split("-"); return s[1].trim()+" - "+s[0].trim()});
}
console.log(result);
A.RAZIK
  • 434
  • 3
  • 8