-4

I'm looking to split a credit card number in 4 groups with 4 digits each.

So for 4111111111111111, to be displayed as 4111 1111 1111 1111.

Keep in mind that this code is already existing, so I don't want to split it while typing.

Thanks a lot!

  • 1
    keep in mind that doing research and being prepared to show your own code attempts is expected before asking – charlietfl Apr 24 '17 at 17:32

1 Answers1

0

With simple logic

'4111111111111111'.match(/.{1,4}/g)

Other way

var str = '4111111111111111';

var parts = [];    
for (var i = 0; i < str.length; i += 4) {
    parts.push(str.substring(i, i + 4));
}    
console.log(parts);
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42