6

I have a string like this: 56f7gg5cx887r7gt8r6t7.

Besides splitting it into an array of one and then looping by two i+2 and creating another array with entries containing two by two.

Is there a simpler way?

The result should be like this: ['56','f7','gg','5c','x8','87','r7','gt','8r','6t','7'].

transilvlad
  • 13,974
  • 13
  • 45
  • 80
  • why do you want to do this? – stackErr Jun 24 '13 at 23:04
  • 3
    See: http://stackoverflow.com/questions/6259515/javascript-elegant-way-to-split-string-into-segments-n-characters-long – Intelekshual Jun 24 '13 at 23:05
  • 7
    Because it sounds like a case of [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Matt Ball Jun 24 '13 at 23:05
  • 1
    Why I need this is irrelevant, besides it would take a sizable long explication to do so, and it is not needed for an answer. I got my answer. Thank you. – transilvlad Jun 24 '13 at 23:12
  • My use case is that I have a date string of the form `'YYYYMMDDhhmmss'`. Splitting that into pairs will make for much easier processing than 6 different substrings. – Teepeemm Oct 10 '19 at 21:02

1 Answers1

22

You can use match:

'56f7gg5cx887r7gt8r6t7'.match(/(..?)/g)
Paul
  • 139,544
  • 27
  • 275
  • 264