Raghav Patnecha
Raghav Patnecha

Reputation: 736

Javascript: how to separate bytes in a string

Lets say I have a bytes like this:

"b'ab' b'xy'"

Now I want to split it into two separate bytes and then will convert it into string. The out should look like:

"b'ab'"   
"b'xy'"

I tried javascript slicing but it won't work as this is a stream of data. So previously if I have these "b'ab' b'xy'" bytes then in the next turn it could be "b'abc' b'xyz'"

Upvotes: 0

Views: 180

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Use byte.split(/\s/) as you have each byte separated by a whitespace. So, you could also do byte.split(' ') but if you have multiple whitespaces between the bytes then it would be better to use \s to be on safe side:

var byte = "b'ab' b'xy'";
var res = byte.split(/\s/);
res.forEach((byte) => {
   document.getElementById('container').innerHTML += '<div> Byte: '+byte+'</div>';
   console.log(byte)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>

Upvotes: 1

Related Questions