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

108
Views
Javascript Remove Negative Decimal Value From String

I am trying to remove the negative and positive decimal value from the string following script remove the positive decimal value from the string however negative is not working

var string = "Test Alpha -0.25 (1-3)"
string = string.replace(/\s*\d+[.,]\d+/g, "");
console.log(string);

above code is returning following output:

Test Alpha - (1-3)

Expected output:

Test Alpha (1-3)

Please help me

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need add the "-" in the regrex condition.

var string = "Test Alpha -0.25 (1-3)"
string = string.replace(/\s*-\d+[.,]\d+/g, "");
console.log(string);

about 4 years ago · Juan Pablo Isaza Report

0

Change the regex statement to match the -, this can be like so:

/\s*-\d+[.,]\d+/g

about 4 years ago · Juan Pablo Isaza Report

0

The sign should be optional (?), and then you can match a set of numbers followed by a . or a ,, followed by another set of numbers, and replace that match. That way you can match both positive and negative numbers with the same expression.

var string = "Test Alpha 0 -0.25 12,31 31 -123.45 (1-3)"
string = string.replace(/( -?\d+([.,]\d+)?)/g, '');
console.log(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!