user2061745
user2061745

Reputation: 305

Add a character between 2 bytes in javascript

What is the easiest way to do this?

I have a string like this.

var string='0ABAFFACBDFE...';

I want to convert it to this.

var newString= '0A,BA,FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..FF,AC,BD,FE ..';

Upvotes: 1

Views: 93

Answers (1)

iConnor
iConnor

Reputation: 20199

Maybe something like

string.match(/.{1,2}/g).join(',');

Split the string by 2 chars, into an Array, then join the array back into a string with the comma on the end of every string in the array.

Upvotes: 5

Related Questions