Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda