I want to catch all binary gaps in bin number with regex. For example if I have 1010010001 I want to get array of these 3 strings: "0", "00", "000" but my regex doesn't use 3-rd "1" for both "101" and "1001" combinations. Here is my regex:
/1(0+)1/g
How I should modify my regex to get desired result ?
You need to make sure the trailing 1
is not consumed by putting it into a positive lookahead:
/1(0+)(?=1)/
^^^^^
See the regex demo.
A matching variation of the same pattern:
/1\K0+(?=1)/
Here, 1
matches 1
and this text is omitted with \K
, then one or more 0
s are matched and 1
presence is checked after the 0
s.
An easy solution would be using split() method to split the binary string using '1', you will get your array of zeros.