[英]Regex to match anything after a delimiter
我有以下情況:
時間格式和一些由分隔符分隔的描述-
(如下)
00:00 - Hello element
我正在使用下面的正則表達式來檢測匹配
/((?:\d{2,}:)?(?:[0-5]\d):(?:[0-5]\d)(\s?-.\w+))/g;
它只匹配空格后的“00:00 - Hello”,它沒有匹配。 這是它的演示https://regex101.com/r/omUEHy/1
此外,如果字符串在同一行中多次匹配,它應該可以工作。
如果我有像00:50 - Hello element 01:50 - Poetry
這樣的文本。 它應該返回00:50 - Hello element
和01:50 - Poetry
提供的正則表達式... /\d{2}:\d{2}\s+.*?(?=\s+\d{2}:|\s+$)/g
... 利用 正向前瞻... (?=\s+\d{2}:|\s+$)
(空白序列兩位數和冒號或空白序列和字符串結尾)...為了定義惰性/非貪婪的終止匹配任何字符...。 .*?
...它確實遵循... \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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.