Format json output by separating in commas

3

I have an object json and I want to divide the result into a comma. Example : 01 , 02 , 03 , 04 , 05

Object json :

[{"rank":"0102030405"}]

HTML output script

$.each(data, function() {
    $('div#content').append(this.rank);
});
    
asked by anonymous 07.10.2016 / 17:00

1 answer

5

Yes, it is possible via RegEx:

console.log("0102030405".match(/.{1,2}/g));

It will generate the following output:

[
  "01",
  "02",
  "03",
  "04",
  "05"
]

The {1,N} parameter specifies capture of groups of size N .

You can then concatenate the results via .join() :

    console.log("0102030405".match(/.{1,2}/g).join(','));

The result will be:

01,02,03,04,05

However, as commented out, it would be interesting if you had this value generated on your backend if possible.

    
07.10.2016 / 17:23