0

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'"

2
  • Bytes? Don't you just want to split on whitespace? Or do you want to specifically match the pattern b'…'? Commented Jun 12, 2018 at 11:11
  • yeah I could just split by whitespace. Commented Jun 12, 2018 at 11:12

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

5 Comments

Thats exactly what I wanted. But lets say instead of console.log I want to write the output to two different divs. For example previously I was doing $("#word").text(e.data); But now as we have split the word can I write the output to different divs
@RaghavPatnecha cool, check the updated answer. And let me know if this is what you want.
Yes. Thanks man. But the thing is could we just remove the foreach loop part. Because as I said I have stream of data. So this foreach is creating many divs which almost covers the whole screen
I mean couldn't it be just like $(#word1).text(byte1) and $(#word2).text(byte2)
Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.