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

162
Views
Why does String(value).replace(/[^0-9.-]/g, '') work?

I saw String(value).replace(/[^0-9.-]/g, '') from a library that described to filter illegal characters, and finally it will return the legal number. Why does this work and what will really be replaced? I feel confused because I think nothing will be replaced.

For example:

String("1.absd").replace(/[^0-9.-]/g, '')
// '1.'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. String(value) will make string from value. So if you use number, this will convert it to string, because function replace is defined only from string.
  2. replace(/[^0-9.-]/g, '') this will replace any matching given regular expression with empty string. In regular expression you have [^0-9.-]. The ^ at start means, that any characters not from this interval will match. So any character other than number, dot or -. Modifier g after slash means, that this regular expression is global and will be applied for all matches

so from "1.absd" the characters "absd" will be replaced with empty string

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!