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

205
Visualizações
Regex between last two characters

I have a querstion about simple regex. I need to get between of these characters: - and ~

My string: Champions tour - To Win1 - To Win2 ~JIM FURYK

When I use this: \-([^)]+\~) it is giving as matched this:

To Win1 - To Win2 ~

But I need this:

To Win2 ~JIM FURYK

Is it possible to this?

My regex is here: https://regex101.com/r/fJBLXb/1/

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Just add \-([^-)]+\~) - dash to not match

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use match as follows:

var input = "Champions tour - To Win1 - To Win2 ~JIM FURYK";
var output = input.match(/- ([^-]+~.*)$/)[1];
console.log(output);

The regex pattern used above says to match:

-          a hyphen
[ ]        a single space
(          capture what follows
    [^-]+  match all content WITHOUT crossing another hyphen
    ~      ~
    .*     all remaining content
)          stop capture
$          end of the string
about 4 years ago · Juan Pablo Isaza Relatório

0

Your \-([^)]+\~) regex matches the leftmost - that is directly followed with one or more chars other than ) (so it matches -, a, §, etc.) and then a ~ char. It does not stop at - chars and thus can match any amount of hyphens.

To match the value after last hyphen you can use

[^\s-][^-]*$

See the regex demo and the regex graph. Details:

  • [^\s-] - a char other than whitespace and -
  • [^-]* - zero or more chars other than -
  • $ - end of string.

See the JavaScript demo:

const text = 'Champions tour - To Win1 - To Win2 ~JIM FURYK';
const match = text.match(/[^\s-][^-]*$/);
if (match) {
    console.log(match[0]);
}

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