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

438
Vistas
Javascript regex to find two characters between two delimitators

EDITED

I need to find two characters between '[' ']' and '/' '/' using Javascript.

I am using this regex:

  ([^.][/[string]]|\/string\/)|(\[(string))|(\/(string))| ((string)\])|((string)\/)

that gets two charactes but gets too one character.

The question is, how can I do to get just two characters?

Also I want to get exactly the two characters inside the string, I mean not just only the exact match.

Eg.

User input: dz It must to find just exact matches that contains "dz", e.g. --> "dzone" but not "dazone". Currently I am getting matches with both strings, "dzone" and "dazone".

Demo: https://regex101.com/r/FEs6ib/1

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could optionally repeat any char except the delimiters between the delimiters them selves, and capture in a group what you want to keep.

If you want multiple matches for /dzone/dzone/ you could assert the last delimiter to the right instead of matching it.

The matches are in group 1 or group 2 where you can check for if they exist.

\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])

The pattern matches:

  • \/ Match /
  • [^\/]*(dz)[^\/]* Capture dz in group 1 between optional chars other than /
  • (?=\/) Positive lookahead, assert / to the right
  • | Or
  • \[ Match [
  • [^\][]*(dz)[^\][]* Capture dz in group 2 between optional chars other than [ and ] -(?=]) Positive lookahead, assert ] to the right

Regex demo

This will match 1 occurrence of dz in the word. If you want to match the whole word, the capture group can be broadened to before and after the negated character class like:

\/([^\/]*dz[^\/]*)(?=\/)|\[([^\][]*dz[^\][]*)(?=])

Regex demo

const regex = /\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])/g;
[
  "[dzone]",
  "/dzone/",
  "/dzone/dzone/",
  "/testdztest/",
  "[dazone]",
  "/dazone/",
  "dzone",
  "dazone"
].forEach(s =>
  console.log(
    `${s} --> ${Array.from(s.matchAll(regex), m => m[2] ? m[2] : m[1])}`
  )
);

If supported, you might also match all occurrences of dz between the delimiters using lookarounds with an infinite quantifier:

(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])

Regex demo

const regex = /(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])/g;
[
  "[adzadzone]",
  "[dzone]",
  "/dzone/",
  "/dzone/dzone/",
  "/testdztest/",
  "[dazone]",
  "/dazone/",
  "dzone",
  "dazone"
].forEach(s => {
  const m = s.match(regex);
  if (m) {
    console.log(`${s} --> ${s.match(regex)}`);
  }
});

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