Lets say I have:
[Radius: 4000 mi.] -- 61362 -- Spring Valley, IL, US
The aim is to get:
Spring Valley, IL
I want to achieve this with RegEx. When I try (?<=--)(.*?)(?=\, US) I can’t seem to get the second group of ‘–‘ out.
>Solution :
You can use the lookbehind, but in between you should not match -- again
(?<= -- )(?:(?! --).)*(?=, US)
const s = `[Radius: 4000 mi.] -- 61362 -- Spring Valley, IL, US`;
const regex = /(?<= -- )(?:(?! --).)*(?=, US)/;
const m = s.match(regex);
if (m) console.log(m[0]);
You can also use a capture group and make the match as specific as you want:
--\s+\d+\s+--\s+(.*?), US\b
const s = `[Radius: 4000 mi.] -- 61362 -- Spring Valley, IL, US`;
const regex = /--\s+\d+\s+--\s+(.*?), US\b/;
const m = s.match(regex);
if (m) console.log(m[1]);