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

172
Visualizações
How to remove all symbols within a given string in Javascript?

Currently I am using the following Javascript code to convert a product title into URL-slug within the base template of my django project.

document.getElementById("title").onkeyup = function () {
    document.getElementById("url_slug").value = document
      .getElementById("title")
      .value.toLowerCase()
      .replaceAll(" ", "-")
      .replaceAll("'", "")
  };

This is using consecutive replaceAll() methods to replace space with dash then remove apostrophes but i would like to prevent all other symbols (e.g. +=()[]$%@#... etc) as well.

Surely there must be a better way? Thanks in advance for any suggestions!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can remove all characters with regex expression /[^A-Za-z0-9]/g

document.getElementById("title").onkeyup = function () {
    document.getElementById("url_slug").value = document
      .getElementById("title")
      .value.toLowerCase().replace(/[^A-Za-z0-9]/g,'')
      .replaceAll(" ", "-")
      .replaceAll("'", "")
  };
about 4 years ago · Juan Pablo Isaza Relatório

0

Something like this?

let stringToReplace = '12+3%42()S$%\|#s'
let desired = stringToReplace.replace(/[^\w\s]/gi, '')

The (^) character is the negation of whatever comes in the set [...],

gi stands for: global and case-insensitive

Plus we put a safelist in there:

In our case digits, chars, underscores (\w) whitespace (\s).

So whatever is out of our whitelist gots replaced with ''

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