Question

If I wanted to match all strings that do not contain "01" from any combinations of 1's and 0's how can I do this? I know how to find if this pattern IS in the string, but how can I find matches that do NOT contain this pattern? Thanks

Was it helpful?

Solution

If I'm not mistaken, this can only happen if the string (of length 1 or bigger):

  1. Contains only 0's
  2. Contains only 1's
  3. Begins with a sequence of 1 or more 1's and then a sequence of 0's

Is there any other possibility?

If that's the case, then

/^(0+|1+0*)$/

Anyway, in the practical sense, I'd just negate the match.

if (!/01/) { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top