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

180
Views
Return numeric pattern with function parameter as its length

I need to return numeric pattern using a function and function has parameter length.

This is what i tried,

export const numericPattern = (length: number) => /[\d]{length}/;

I just want to return the pattern with given length and if length is not given as parameter, i need to return only /[\d]/.

Thanks

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

0

const numericPattern = (length) => {
  const tail = length ? "{" + length + "}" : "";
  return new RegExp(`[\\d]${tail}`);
};

console.log(numericPattern(1)); // -> /[\d]{1}/
console.log(numericPattern()); //  -> /[\d]/
about 4 years ago · Juan Pablo Isaza Report

0

This should work:

function rx(l){
 return new RegExp("[\\d]"+(l===undefined?"":`{${l}}`),"g");
}

const tst=["7864","abc"],
      larr=[undefined,0,1,3];

tst.forEach(t=>{
 console.log("String:",t);
 larr.forEach(l=>{
  let r=rx(l);
  console.log(l,r.toString(),t.match(r));
 })
});

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!