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

241
Visualizações
How to match only the first occurrence of an alphanumeric character, preceded by a non-alphanumeric character?

How would I construct a regular expression to yield the index of an alphanumeric character that is preceded by a non-alphanumeric character?

Examples:

  1. (Special Edition) --> should match only 'S' and yield an index of 1
  2. Special Edition --> should not match at all
  3. "{(Special Edition)}" --> should match 'S' and yield an index of 3

I know that JavaScript can provide the index of the match like so:

const index = string.match(/regular_expression/)?.index || 0;

I cannot figure out how to construct the an expression that does what I have described.

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

0

If this part (S should yield index 1, I think this part {(S should yield index 2 instead of 3.

You can match both characters and capture the second character using a capture group, and then add 1 to get the index.

You can use a lookbehind assertion if that is supported and then you don't need the extra addition of 1.

As a pattern you can match:

[^a-zA-Z\d\s]([a-zA-Z\d])

The pattern matches:

  • [^a-zA-Z\d\s] Match a single char not being alphanumeric or a whitespace char
  • ([a-zA-Z\d]) Capture a single alphanumeric char in group 1

See a regex demo.

const regex = /[^a-zA-Z\d\s]([a-zA-Z\d])/;

[
  "(Special Edition)",
  "Special Edition",
  "{(Special Edition)}",
].forEach(s => {
  const m = s.match(regex);
  if (m) {
    console.log(`${s} --> ${m[1]} index: ${m.index + 1}`);
  }
})

about 4 years ago · Juan Pablo Isaza Relatório

0

Use positive lookbehind and String.prototype.search method:

'(Special Edition)'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 1

'Special Edition'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // -1

'{(Special Edition)}'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 2
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