1

I have a string like '123,45,6,7' and want to split it on separate digit: [1, 2, 3, 4, 5, 6, 7]

I know there is a way to split string by complex regex like this:

'1 2 3,4a5,6,7'.split(/[^\d]/) -> [1,2,3,4,5,6,7].

Is there way to split this string by regex, something like:

'123,45,6,7'.split(/[^\d]|empty string/)?

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Max
  • 43
  • 9

1 Answers1

1

You could split by positive lookahead with a digit.

console.log('123,45,6,7'.split(/\D|(?=\d)/));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392