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

215
Vistas
Why does this negative lookahead Unicode regex not work in JavaScript when direct match does?

I am trying to validate for filename or directory portions of a string that will eventually be used in a URL and want to reject non Unicode plus other characters, the regex is returning null bytes.

Given this string as input:

զվարճ?անք9879#jhkjhkhl!kjljlkjlkjj()+======\_ew.html

/(?![\p{L}]|[\p{N}]|[\._-~])/gu

JavaScript returns correct invalid character matches, but is selecting a null byte for every character matched and not the full character.

If I run the opposite and try to match on characters that are ok instead of not ok:

/[\p{L}]|[\p{N}]|[\._-~]/gu

JavaScript returns matches and selects each valid character as expected, no null byte matches.

Each pattern has the /u flag. I don't understand the difference in behavior. Tested this in the latest Chrome (update 100 as of post date), Safari, and Firefox and they all behave the same.

Is there some flag or operator that the first regex is missing or is this a JavaScript bug / limitation?

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

0

You are not matching, only asserting. You can either match a single character right after the assertion and bundle the alternation to a single character class:

(?![\p{L}\p{N}._-~]).

Regex 101 demo

Or you can match 1 or more times the opposite using a negated character class starting with [^

[^\p{L}\p{N}._-~]+

Regex 101 demo

Note that this part in the character class _-~ denotes a range instead of chars _ - ~

If you want to match the - char, you can either escape it or place it at the start or end of the character class.

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