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

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

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 Denunciar

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