Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda