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
const numericPattern = (length) => {
const tail = length ? "{" + length + "}" : "";
return new RegExp(`[\\d]${tail}`);
};
console.log(numericPattern(1)); // -> /[\d]{1}/
console.log(numericPattern()); // -> /[\d]/
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));
})
});