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

279
Visualizações
Regex: Equivalent expression without using positive lookbehind

I need to split 4d6/2d6/1d6 matching /. However, in order to split it I need to have specifically \d preceding / and another \d|d succeeding /, so other expressions like 1d6+db/2 don't get split. I've come to the solution of using positive lookbehind, but I need my regex to be valid in IOS systems too. The solution I've come up with is /(?<=\d)\/(?=\d|d)/g. Is there a way to create an equivalent to this expression without using positive lookbehind?

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

0

You can use an extracting, matching approach rather than splitting:

/(?:\/|^)(.*?\d)(?=\/\d|$)/g
/(?:\/|^)([\w\W]*?\d)(?=\/\d|$)/g

See the regex demo. Your value is in Group 1. The regex with [\w\W] matches across lines, too (as . does not match line breaks by default).

Details:

  • (?:\/|^) - a non-capturing group that matches either / or start of string
  • (.*?\d) - Group 1: any zero or more chars (here, other than line break chars) as few as possible, and then a digit
  • (?=\/\d|$) - a location that is immediately followed with / + digit, or end of string.

In JavaScript, you can either use const matches = Array.from(text.matchAll(/(?:\/|^)(.*?\d)(?=\/\d|$)/g), x => x[1]), or - if you cannot use this syntax - a more verbose legacy extraction like

var s = "4d6/2d6/1d6";
var re = /(?:\/|^)(.*?\d)(?=\/\d|$)/g;
var matches=[], m;
while(m=re.exec(s)) {
  matches.push(m[1]);
}
console.log(matches)

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