Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Regular Expression to find a group of strings between two characters

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you're open to considering a non-regex approach, you can split the string on the hyphens and spaces (' -- '), take the element at index 2 (the city, state, country), split that by the comma limiting the results to 2, and then rejoining by a comma, and you have what you're looking for.

Easier to read than a regex approach, and likely easier for other developers in your code base to understand what is happening.

const s = '[Radius: 4000 mi.] -- 61362 -- Spring Valley, IL, US';

const cityState = s.split(' -- ')[2]?.split(',', 2).join(',');

console.log(cityState);

about 4 years ago · Juan Pablo Isaza Report

0

You can use the lookbehind, but in between you should not match -- again

(?<= -- )(?:(?! --).)*(?=, US)

Regex demo

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

Regex demo

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]);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!