To achieve this using only regex, it's important not to include the terminating 1 in the match result, as it might be needed for a subsequent match.
Instead, ensure that there is a 1 immediately following the match without capturing it.
If you want to retain the ending 1 in the output, adjust the match
result accordingly:
console.log(
(101001000).toString().match(/10+(?=1)/g).map(m => m + "1")
)
Some additional points to consider:
It is unnecessary to use the Number
function since 101001000 is already a number. The extra step is mainly needed to clarify the dot in .toString()
. Simply adding parentheses resolves the ambiguity. Another option is to add an extra dot: 101001000..toString()
will also work.
Square brackets are not required around 0 in your regex, nor do you need to enclose the entire expression in parentheses.