Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!