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

211
Visualizações
JS regexp positive lookbehind no match

Here's the string and the sentence I want to match (extract).

let pigs = "\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

" Pigs are 5555 courteous."

Here's two versions of my code. When I check in on various regexp-check websites, it gives the desired match. But when I run in the browser console, it gives null. I have the latest Chrome version. Why doesn't the browser console output the match here?

pigs.match(/(?<=\\X)[^\\X]*5555[^\\X]*(?=\\X)/g);
pigs.match(/(?:\\X)[^\\X]*5555[^\\X]*(?:\\X)/g); 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use following pattern:

(?<=\X)(?:.(?!\X))+5555.+?(?=\X)

What I changed is: (?:.(?!\X))+ - which is non-capturing group and expression matches one or more characters, which are not followed by \X.

Also, you wrongly used [^\\X] - it's just negated class, it will match any character other than \ or X.

Below JS example:

let pigs = "\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

let match = pigs.match(/(?<=\X)(?:.(?!\X))+5555.+?(?=\X)/g)

console.log(match)

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