简体   繁体   中英

Regex to match anything after a delimiter

I have the following case:

Time format and some description which is separated by a delimiter - (as like below)

00:00 - Hello element

I was using the below regex to detect the match

/((?:\d{2,}:)?(?:[0-5]\d):(?:[0-5]\d)(\s?-.\w+))/g;

it matches only "00:00 - Hello" after space it's not getting match. Here is the demo of it https://regex101.com/r/omUEHy/1

Also, If the string matches multiple times in a same line it should work.

If I have text like as 00:50 - Hello element 01:50 - Poetry . It should return 00:50 - Hello element and 01:50 - Poetry

The provided regex... /\d{2}:\d{2}\s+.*?(?=\s+\d{2}:|\s+$)/g ... makes use of a positive lookahead ... (?=\s+\d{2}:|\s+$) (either whitespace sequence double digit and colon or whitespace sequence and end of string)... in order to define the termination of the lazy/non greedy match of any character... .*? ... which does follow the opening matching sequence of... \d{2}:\d{2}\s+ .

 const multilineSample = `00:50 - Hello element 01:50 - Poetry 00:50 - Hello element and another element 01:50 - Poetry 00:50 - Hello element and another element 01:50 - Poetry and prose 00:50 - Hello element 01:50 - Poetry `; const regXTimeAndEntry = /\d{2}:\d{2}\s+.*?(?=\s+\d{2}:|\s+$)/g; console.log( multilineSample.match(regXTimeAndEntry) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

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