0

How can I achieve the following in typescript angular?

var specimenNumber = '19S20010';

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

// expected output : 19S-2001-0
  • Does this answer your question? [Phone mask with jQuery and Masked Input Plugin](https://stackoverflow.com/questions/11633726/phone-mask-with-jquery-and-masked-input-plugin) – Peter Hassaballah Mar 08 '22 at 15:17

2 Answers2

0

I found this in taiga-ui library

Typescript code

demo

Dharman
  • 30,962
  • 25
  • 85
  • 135
helloworld
  • 151
  • 1
  • 6
0

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;
}    
Harshit Rastogi
  • 1,996
  • 1
  • 10
  • 19