'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
Solution 1:[1]
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; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Seliger |
