简体   繁体   English

如何在Javascript中使用REGEX分割此字串

[英]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: 我已经尝试使用使用日期的split方法:

/((..\/..\/....)-(..\/..\/....) \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... 如果您还有其他问题或在理解我的代码的任何部分时遇到困难,请随时发表评论...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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