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

139
Views
How do I leave a fixed value in a mask with regex?

I have a time mask, it accepts +1000h, but I wanted to set:00 after the hours

    return varTemp
      .replace(/\D/g, "")
      .replace(/(\d{2,4})(\d{2}$)/, "$1:$2")
  };

In this mask, : stays between hours and minutes. But I wanted to leave :00 fixed, without the minutes.


const maskHours = (value) => {
  return (
    value
      .replace(/\D/g, "")
      .replace(/(\d{2})/, "$1:00")
  );
};

In this mask, the value :00 is fixed, but the result is not good.

Thanks to whoever helps me!

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

0

Use

const maskHours = (value) => {
  return (
    value
      .replace(/\D/g, "")
      .replace(/^(\d{2,4})\d+/, "$1:00")
  );
};

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d{2,4}                  digits (0-9) (between 2 and 4 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
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!