I need a regex with which I could find if there is any assignation inside conditions.
I will use this regex on .php files to find mistakes such as :
<?php
if($foo = "bar") { // instead of $foo == "bar"
//...
}
I found a question similar to mine that helped me resolve this problem with this regex :
/ifs*(.+[^!=<>]=[^=]/
However, it also matches where there is an ‘=’ inside a string which is not what I wanted :
<?php
if($foo != '=') {
//...
}
Note that it should also work on complex conditions with many comparisons such as :
if($foo == 0 && ($bar == 1 || $fooBar = 1) { // Instead of $fooBar == 1
// ...
}
It is quite a challenging regex to find, and I would be very gratefull if I could get some help !
2
Answers
You can try limiting the set of characters that identify the variable name, so as not to use
.
which also includes equality signs:If there is always a variable in your controls, and if you use double conditions such as
if($foo == "bar" & $tmp = 'tmp')
, you can reduce the regex like this:[$w-]+[^!=<>]=[^=]
You can use this
Example