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

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

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