I have this string:
|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|
I want to match if CT block (have CT; between |) has ID=1, i tried:
/\|CT;.*?ID=1;(?=.*\|)/
But doesn't working.
Code:
let string = '|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|'
console.log(string.match(/\|CT;.*?ID=1;(?=.*\|)/g))
// return ['|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;']
// expected null
Someone can help me?
CodePudding user response:
You can use
/\|CT;[^|]*ID=1;/
See the regex demo.
Details:
\|CT;
-|CT;
string[^|]*
- zero or more chars other than a|
charID=1;
- a fixed string.
See a JavaScript demo:
const strings = [
'|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|',
'|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=1;AF=123|BD;GF=0;ID=1;AF=124|'
]
for (const string of strings) {
console.log(string.match(/\|CT;[^|]*ID=1;/)?.[0]);
}