I am looking for a RegExp to stop matching after a certain keyword.
This is my Input:
blabl
texta 1,40
textb 3.50
textc 6,90
blabal 2 3
ddd 3 jj d
key textd textf
2,30 4,70 5,90
What I want to match:
blabl
texta 1,40
textb 3.50
textc 6,90
blabal 2 3
ddd 3 jj d
key textd textf
2,30 4,70 5,90
What my RegExp currently looks like
[\d]{1,2}[.,][\d]{2}
What I tried to fix the problem
[\d]{1,2}[.,][\d]{2}(?=key)
So I was looking for something like this:
(What I want to match)(anything)(keyword)
I tried the following RegExp:
([\d]{1,2}[.,][\d]{2}(.|\n)+(?=key))
But now it matches:
texta 1,40
textb 3.50
textc 6,90
blabal 2 3
ddd 3 jj d
key textd textf
2,30 4,70 5,90
I read about the non-capturing groups, which seemed to solve my problem. So I tried:
([\d]{1,2}[.,][\d]{2}(?>(.|\n)+)(?=key))
But that does not match anything at all.
I feel like I'm almost there, but I could really use some help. Any idea what I'm missing is appreciated. If you have any questions regarding my problem, please do not hesitate to ask.
Thanks in advance for your time and effort.
You may use
/\d+[,.]\d+(?=[\s\S]*key)/g
See the regex demo.
Details
\d+
- 1+ digits[,.]
- a ,
or .
\d+
- 1+ digits(?=[\s\S]*key)
- a positive lookahead that requires any 0+ chars followed with a word key
immediately to the right after the last 1+ digits matched.JS demo:
var s = "blabl\n\ntexta 1,40\ntextb 3.50\ntextc 6,90\n\nblabal 2 3\nddd 3 jj d \n\nkey textd textf\n2,30 4,70 5,90";
var rx = /\d+[,.]\d+(?=[\s\S]*key)/g;
console.log(s.match(rx));