Reputation: 8042
I am trying to make a regex that will match a number of x
's that is a power of two. I am using JavaScript. I tried this one:
^(x\1?)$
but it doesn't work. Shouldn't the \1
refer to the outer parathesis so it should match xx
, and therefore also xxxx
, etc.?
I tried a simpler one that I thought would match x and xx:
^((x)|(\2{2}))$
but this only matches x
.
What am I doing wrong?
Upvotes: 1
Views: 92
Reputation: 64563
You can't do "recursive backreferences". At least, it is not so easy.
I'm no sure that you need recuresive regular expressions here. May be you could just count number of the characters in the string and check if it is equal to a power of two?
But if you really need recursive regular expressions (I'm almost sure, you don't), you can check this question:
and this blog
Upvotes: 2