2

I want to split a string contains letters followed by digits, the separator should be the next letter.

'C3B1A60' ===> ['C3', 'B1', 'A60']

I've tried to use SPLIT with RegEx and it worked fine except it produces empty string in between:

var splits = 'C3B1A60'.split(/([A-Z]\d+)/);
// [ '', 'C3', '', 'B1', '', 'A60', '' ]

I know I can workaround to remove them, but is there a straightforward way of doing it?!

iseenoob
  • 339
  • 1
  • 8

1 Answers1

1

Just use .filter(Boolean) in order to keep only desired items.

var str = 'C3B1A60';
var splits = 'C3B1A60'.split(/([A-Z]\d+)/).filter(Boolean);
console.log(splits);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128