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

260
Views
JavaScript: How to reduce IFs with REGEX

I'm working on the following function which I had to add a few IFs in case the string was shorter than the format the REGEX groups was expecting:

function getFormatedCPF(unformatedValue) {
    const onlyDigits = unformatedValue.toString().replace(/[^\d]/g, "").substring(0, 11);

    if (onlyDigits.length > 9) return onlyDigits.replace(/(\d{3})(\d{3})(\d{3})/, "$1.$2.$3-");
    if (onlyDigits.length > 6) return onlyDigits.replace(/(\d{3})(\d{3})/, "$1.$2.");
    if (onlyDigits.length > 3) return onlyDigits.replace(/(\d{3})/, "$1.");

    return onlyDigits;
}

Is there a way to remove the IFs and get the same output with just Regex? I'm looking for something like: complete: 999.999.999-99 partial: 999.999.9 Because the function will be called when the person is typing.

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

0

You can use

const rx = /^(\d{3})(\d{1,3})?(\d{1,3})?(\d{1,2})?.*/;

$('body').on('input', '.val', function(e) {
  this.value = this.value.replace(/\D+/g,'')
    .replace(rx, (_,w,x,y,z) =>
      z ? `${w}.${x}.${y}-${z}` :
      y ? `${w}.${x}.${y}` :
      x ? `${w}.${x}` : w);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="val" />

Note that ^(\d{3})(\d{1,3})?(\d{1,3})?(\d{1,2})?.* regex matches and captures into Group 1 all three digits (else, there is no sense adding .), then one, two or three digits are captured into an optional Group 2 (one digit must be there, or again, there is no need to insert a separator), same goes for Group 3 and there are only one or two digits that are captured into Group 4. The .* at the end matches anything there is left to trim it off.

The replacement is done within an arrow function, where the replacement text is built dynamically, based on which group matched or not.

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!