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

171
Vistas
RegExp matching @ mention tags with optional space

I'm working with some code that uses the regex /(@\w+) ?/g for matching tagged mentions

This will match the following text returning the groups correctly without the additional space

testing @one and @two and @three

If I remove the optional space ? the regex /(@\w+)/g appears to return the same matching groups - for the above example and any other test strings I've tried

Is there some subtle difference or edge case that I might be missing between these two version of the regex?

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

0

If there is a space the regex will match it, but it won't affect the matching groups because the space isn't in any group. Only the whole match will have the space.

console.log(...'testing @one and @two and @three'.matchAll(/(@\w+) ?/g));
// [ "@one ", "@one" ]
// [ "@two ", "@two" ]
// [ "@three", "@three" ]

The first element of each result array is the whole match and the second element is what is matched by the group (@\w+).

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