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

441
Visualizações
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 Respostas
Responde à pergunta

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 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