Split String using multiple Selectors

Ambika Tewari

I have below string :

var deptInfo = "Flight-1 08:30~Flight-1 10:45|Flight-2 15:15~Flight-2 17:30"

I want to have info in the format:

var timeSlot = ["08:30-10:45","15:15-17:30"]

I am trying to split the deptInfo like this:

var timeSlot = deptInfo.split("(Flight-1 )|(~Flight-1 )").join('-').split('|').join(',').split("(Flight-2 )|(~Flight-2 )").join('-');

var timeSlot = deptInfo.split(/Flight-1 ~Flight-1 /).join('-').split('|').join(',').split(/Flight-2 ~Flight-2 /).join('-');

But this is not delivering the expected results. Can someone please help?

Wiktor Stribiżew

You may use the following pattern to extract the necessary bits and then post-process the matches to get the necessary output:

/(\d{2}:\d{2})~Flight-\d+\s*(\d{2}:\d{2})/g

See the regex demo. Note you may even wrap the pattern with \b (word boundaries) if you want to make sure the first and last 2 digits are not preceded/followed with a word char, /\b(\d{2}:\d{2})~Flight-\d+\s*(\d{2}:\d{2})\b/g.

Details

  • (\d{2}:\d{2}) - Group 1: 2 digits, :, 2 digits
  • ~Flight-\d+ - a ~Flight- substring and then 1+ digits
  • \s* - 0+ whitespaces
  • (\d{2}:\d{2}) - Group 2: 2 digits, :, 2 digits

JS usage:

var rx = /(\d{2}:\d{2})~Flight-\d+\s*(\d{2}:\d{2})/g;
var s = "Flight-1 08:30~Flight-1 10:45|Flight-2 15:15~Flight-2 17:30";
var m, res = [];
while (m = rx.exec(s)) {
    res.push(m[1] + "-" + m[2]);
} 
console.log(res);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using String.split to split with multiple conditions

From Dev

Multiple selectors using this and variable

From Dev

Using multiple ">" CSS selectors

From Dev

String.split() using multiple character delimeter

From Dev

Split string using multiple nested delimiters

From Dev

Split string with multiple delimiters using strtok in C

From Dev

Split String using multiple delimiters in one step

From Dev

String.split() using multiple character delimeter

From Dev

Using "on" method to define multiple events for multiple selectors

From Dev

jquery using multiple selectors in Internet Explorer

From Dev

Using multiple CSS selectors for the same ArticleItem in Scrapy

From Dev

jquery using multiple selectors in Internet Explorer

From Dev

Jquery using multiple selectors to show all at a time

From Dev

Multiple selectors using have_selector

From Java

Split string by multiple delimiters

From Dev

Split a string on multiple characters

From Dev

Split a string on multiple characters

From Dev

How to split a string into a list with multiple values, using Erlang?

From Dev

Using SparkR, how to split a string column into 'n' multiple columns?

From Dev

Julia: Using split to construct string arrays with multiple columns

From Dev

Split string in to multiple columns using reqular expression in sql

From Dev

Split String with multiple String Delimiter

From Dev

Unable to split String using |

From Dev

Using string split in Ruby?

From Dev

Split string using reges

From Dev

Split string using IFS

From Dev

javascript split string multiple ways

From Java

Split string with multiple delimiters in Python

From Dev

Split a string based on multiple delimiters

Related Related

HotTag

Archive