如果有特定的分隔符,则排除字符串

时间:2018-07-25 09:48:42

标签: javascript jquery regex

我有以下格式的字符串,如果遇到定界符“ NonStop”,而不是通过“连接”或“通过”,我想将其推送到数组中。如果遇到“正在连接”或“通过”,则排除。

STRING:

var timeSlots = "01:30 - 04:15NonStop04:00 - 06:50NonStop06:05 - 08:50NonStop07:10 - 10:05NonStop08:30 - 11:20NonStop10:10 - 13:00NonStop10:30 - 13:20NonStop12:30 - 15:20NonStop13:00 - 15:50NonStop14:30 - 17:25NonStop16:55 - 19:55NonStop18:55 - 21:50NonStop20:00 - 22:50NonStop21:05 - 23:55NonStop21:15 - 00:15NonStop22:10 - 01:00NonStop23:30 - 02:20NonStop02:40 - 07:50Connecting02:40 - 08:35Connecting05:00 - 10:45Connecting05:00 - 11:45Connecting05:15 - 11:15Connecting05:15 - 10:00Connecting05:25 - 12:10Connecting05:35 - 12:05Connecting05:45 - 11:30Connecting06:05 - 11:45Connecting06:10 - 11:50Connecting06:20 - 12:45Connecting06:25 - 11:25Connecting07:05 - 13:10Connecting07:10 - 13:20Connecting07:30 - 12:45Connecting07:30 - 14:10Connecting08:25 - 14:10Connecting09:20 - 15:00Connecting09:20 - 15:45Connecting10:00 - 16:00Connecting10:20 - 16:45Connecting11:15 - 16:30Connecting11:55 - 18:10Connecting12:05 - 18:15Connecting12:05 - 17:10Connecting12:25 - 18:25Connecting12:50 - 18:40Connecting12:55 - 19:00Connecting12:55 - 18:00Connecting14:25 - 19:25Connecting14:35 - 20:05Connecting14:50 - 21:05Connecting15:25 - 20:55Connecting15:35 - 22:15Connecting16:10 - 21:50Connecting16:55 - 23:30Connecting16:55 - 22:55Connecting17:25 - 23:35Connecting17:35 - 23:30Connecting18:05 - 00:35Connecting18:20 - 00:40Connecting18:30 - 23:45Connecting18:30 - 23:05Connecting18:45 - 23:30Connecting19:25 - 01:35Connecting20:25 - 01:50Connecting20:25 - 01:45Connecting21:30 - 04:15Connecting22:45 - 04:20Connecting"


var timeRetArray = timeSlots.split(/NonStop|Connecting|Through/);

这个timeRetArray将包含所有时隙,但是如果要跟在“ NonStop”字符串之后,我想包含timeslsots。 例如,此数组不应具有02:40-07:50、02:40-07:50,05:00-10:45时隙

2 个答案:

答案 0 :(得分:0)

match将返回时间范围仅在“ NonStop”之前的数组:

var timeRetArray = timeSlots.match(/\d{2}:\d{2} - \d{2}:\d{2}(?=NonStop)/g);

答案 1 :(得分:0)

您可以使用以下正则表达式:

(\d{2}:\d{2}\s-\s\d{2}:\d{2})(?=NonStop)

JavaScript代码:

var time_slots = "01:30 - 04:15NonStop04:00 - 06:50NonStop06:05 - 08:50NonStop07:10 - 10:05NonStop08:30 - 11:20NonStop10:10 - 13:00NonStop10:30 - 13:20NonStop12:30 - 15:20NonStop13:00 - 15:50NonStop14:30 - 17:25NonStop16:55 - 19:55NonStop18:55 - 21:50NonStop20:00 - 22:50NonStop21:05 - 23:55NonStop21:15 - 00:15NonStop22:10 - 01:00NonStop23:30 - 02:20NonStop02:40 - 07:50Connecting02:40 - 08:35Connecting05:00 - 10:45Connecting05:00 - 11:45Connecting05:15 - 11:15Connecting05:15 - 10:00Connecting05:25 - 12:10Connecting05:35 - 12:05Connecting05:45 - 11:30Connecting06:05 - 11:45Connecting06:10 - 11:50Connecting06:20 - 12:45Connecting06:25 - 11:25Connecting07:05 - 13:10Connecting07:10 - 13:20Connecting07:30 - 12:45Connecting07:30 - 14:10Connecting08:25 - 14:10Connecting09:20 - 15:00Connecting09:20 - 15:45Connecting10:00 - 16:00Connecting10:20 - 16:45Connecting11:15 - 16:30Connecting11:55 - 18:10Connecting12:05 - 18:15Connecting12:05 - 17:10Connecting12:25 - 18:25Connecting12:50 - 18:40Connecting12:55 - 19:00Connecting12:55 - 18:00Connecting14:25 - 19:25Connecting14:35 - 20:05Connecting14:50 - 21:05Connecting15:25 - 20:55Connecting15:35 - 22:15Connecting16:10 - 21:50Connecting16:55 - 23:30Connecting16:55 - 22:55Connecting17:25 - 23:35Connecting17:35 - 23:30Connecting18:05 - 00:35Connecting18:20 - 00:40Connecting18:30 - 23:45Connecting18:30 - 23:05Connecting18:45 - 23:30Connecting19:25 - 01:35Connecting20:25 - 01:50Connecting20:25 - 01:45Connecting21:30 - 04:15Connecting22:45 - 04:20Connecting";

console.log(time_slots.match(/(\d{2}:\d{2}\s-\s\d{2}:\d{2})(?=NonStop)/g));

测试字符串:

01:30 - 04:15NonStop04:00 - 06:50NonStop06:05 - 08:50NonStop07:10 - 10:05NonStop08:30 - 11:20NonStop10:10 - 13:00NonStop10:30 - 13:20NonStop12:30 - 15:20NonStop13:00 - 15:50NonStop14:30 - 17:25NonStop16:55 - 19:55NonStop18:55 - 21:50NonStop20:00 - 22:50NonStop21:05 - 23:55NonStop21:15 - 00:15NonStop22:10 - 01:00NonStop23:30 - 02:20NonStop02:40 - 07:50Connecting02:40 - 08:35Connecting05:00 - 10:45Connecting05:00 - 11:45Connecting05:15 - 11:15Connecting05:15 - ...

输出:

Match 1
Full match  0-13    `01:30 - 04:15`
Match 2
Full match  20-33   `04:00 - 06:50`
Match 3
Full match  40-53   `06:05 - 08:50`
Match 4
Full match  60-73   `07:10 - 10:05`
Match 5
Full match  80-93   `08:30 - 11:20`
Match 6
Full match  100-113 `10:10 - 13:00`
Match 7
Full match  120-133 `10:30 - 13:20`
Match 8
Full match  140-153 `12:30 - 15:20`
Match 9
Full match  160-173 `13:00 - 15:50`
Match 10
Full match  180-193 `14:30 - 17:25`
Match 11
Full match  200-213 `16:55 - 19:55`
Match 12
Full match  220-233 `18:55 - 21:50`
Match 13
Full match  240-253 `20:00 - 22:50`
Match 14
Full match  260-273 `21:05 - 23:55`
Match 15
Full match  280-293 `21:15 - 00:15`
Match 16
Full match  300-313 `22:10 - 01:00`
Match 17
Full match  320-333 `23:30 - 02:20`

演示:https://regex101.com/r/NyQCkO/1