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

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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