skip to Main Content

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


  1. You can try limiting the set of characters that identify the variable name, so as not to use . which also includes equality signs:

    ifs*({1}[$w-]+[^!=<>](=){1,1}?[^=]
    

    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-]+[^!=<>]=[^=]

    Login or Signup to reply.
  2. You can use this

    /ifs*(s*[^!=<>n]*=[^=n]*)/
    

    enter image description here

    Example

    FYI: I don’t know what you’re trying to archive. I have provided a solution based on your question. (Regex)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search