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

305
Visualizações
Remove all special consecutive characters

Hopefully a simple one but I can't find any exact thread on SO for this particular instance:

If I have a string with multiple special characters in a row i.e 'this-is--a---string' I want to remove all duplicate non alphanumeric characters with regex so it would end up as 'this-is-a-string'

The closest i've found is .replace(/(.)\1+/g, '$1') but this removes duplicate letters rather than just special characters.

On a side note if anyone knows how to remove any non alphanumeric characters from the end of a string with regex then that would be really useful too!

Thanks in advance!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Since you want to collapse only repeated non alphanumeric characters, it should be enough to change your regex replacing the . character class with something that will negate the alphanumeric group [^a-zA-Z0-9].

subject = "a---------------bbbbbb6times_______@@@@@@";

var myregexp = /([^a-zA-Z0-9])\1+/mg;
result = subject.replace(myregexp, "$1");

console.log(result);

about 4 years ago · Santiago Trujillo 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