So I have been practicing and reading about assertions, and ran into this problem:
const text14 = "123 456 7890";
const pattern12 = /\d+(?!0)/g;
let matches12 = text14.match(pattern12);
console.log(matches12);
the output is supposed to be ['123', '456'] Yet it isn't. its ['123', '456', '7890']
After tinkering with it a bit I realized that when I put a space on the assertion as well as on the string itself, it removed, yet only the 9.
const text14 = "123 456 789 0";
const pattern12 = /\d+(?! 0)/g;
let matches12 = text14.match(pattern12);
console.log(matches12);
Ouput:
['123', '456', '78', '0']
This made me believe that there is a different way in which assertion works with numbers. The desired outcome I've been trying to get is to turn the original "123 456 7890" into ['123', '456'] using the negative lookahead assertion: 'x(?!y)'.
The regular expression
/\d+(?!0)/gwill match all substrings that:The problem is that 0 is itself a digit. The regex keeps accepting digits including zeroes until it encounters a character that isn't a digit, and only then does it check that the character after that is not a zero. So the negative lookahead never comes into play.
You might be tempted to simply use negative lookbehind instead, like so:
But in such a case the regular expression will simply stop before the zero instead, and not discard the entire sequence as you wished. Instead, you should first match a sequence of digits that ends in a nonzero digit, then make sure there isn't another digit after that.
Keep in mind that the way you write a regular expression can affect its performance. I would not expect this one to be very efficient. A more naïve approach would be to simply accept any sequence of digits and then filter the results with JavaScript: