Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split a string to array containing pairs of two [duplicate]

Tags:

javascript

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'].

like image 482
transilvlad Avatar asked Jun 24 '13 23:06

transilvlad


1 Answers

You can use match:

'56f7gg5cx887r7gt8r6t7'.match(/(..?)/g)
like image 63
Paul Avatar answered Sep 27 '22 20:09

Paul