0

I want to split a string that represent a sequence of bits (only "0" and "1") in different strings with a length of 8.

I would like to use the javascript split function, and I know that It is possible to use a regex to achieve that division. I have something close to a solution:

"10111001110001011011".split(/([01]{8})/)

But it return an array with five elements, where there are two empty:

(5) ["", "10111001", "", "11000101", "1011"]

What should be the right regex to use in split to get one array with only the non empty strings. (I don´t want to use another function to filter the result...)

2
  • 1
    I don't think .split can do this. Try .match instead. Commented Aug 27, 2017 at 2:06
  • ok with match function! tks Commented Aug 27, 2017 at 2:12

1 Answer 1

1

As Alexander pointed out at his comment, you need to use .match, like this:

var binaryNumber = "1011100111000101101100110101110111011100010101";

console.log(binaryNumber.match(/.{1,8}/g));

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

Comments

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.