简体   繁体   中英

How do I split this string using REGEX in Javascript

I have the following string:

01/12/2015-04/27/2015 Lecture Monday, Wednesday, Friday 12:00PM - 12:50PM, CoolGuy Hall, Room 006 01/12/2015-04/27/2015 Laboratory Thursday 10:50AM - 12:05PM, Epic Science, Room 121

I am trying to parse it so I can get objects like so

{
 days: ["Monday", "Wednesday", "Friday"]
 start: "12:00PM"
 end: "12:50PM"
 location: "CoolGuy Hall, Room 006"
}

and

{
 days: ["Thursday"]
 start: "10:50AM"
 end: "12:05PM"
 location: "Epic Science, Room 121"
}

Is there anyway to do this cleanly? I have tried using the split method using the dates:

/((..\/..\/....)-(..\/..\/....) \w+)/

to no avail and as you can see above my regex skills are fairly limited.

Thank you!

Following code is working for me... check is this you are trying to do...??

var data = "01/12/2015-04/27/2015 Lecture Monday, Wednesday, Friday 12:00PM - 12:50PM, CoolGuy Hall, Room 006 01/12/2015-04/27/2015 Laboratory Thursday 10:50AM - 12:05PM, Epic Science, Room 121"

data.replace(/\d{2}\/\d{2}\/\d{4}\-\d{2}\/\d{2}\/\d{4} ([^\s]+) ([^\d]+) ([^\s]+) \- ([^,]+), ([^,]+), Room ([^\s]+)/g, "{\n\tdays: [$2]\n\tstart: \"$3\"\n\tend: \"$4\"\n\tlocation: \"$5, Room $6\"\n}\n");

Output:

{
    days: [Monday, Wednesday, Friday]
    start: "12:00PM"
    end: "12:50PM"
    location: "CoolGuy Hall, Room 006"
}
{
    days: [Thursday]
    start: "10:50AM"
    end: "12:05PM"
    location: "Epic Science, Room 121"
}

Feel free to comment if you have any further questions or face difficulties to understand any part of my code...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM