Home > Enterprise >  Alphanumeric string format to specific pattern in Angular
Alphanumeric string format to specific pattern in Angular

Time:09-30

How can I achieve the following in typescript angular?

var specimenNumber = '19S20010';

// format to specific pattern 
format(specimenNumber, "***-****-*");

// expected output : 19S-2001-0

CodePudding user response:

With the help of this you can use any pattern with no regex used:

var specimenNumber = '19S20010';

// format to specific pattern 
console.log(format(specimenNumber, "***-****-*"));

function format(number, mask) {
   var s = '' number, r = '';
   for (var im=0, is = 0; im<mask.length && is<s.length; im  ) {
     r  = mask.charAt(im)=='*' ? s.charAt(is  ) : mask.charAt(im);
   }
   return r;
}    

CodePudding user response:

I found this in taiga-ui library

Typescript code

demo

  • Related