Mark Manders
Giga Patron

Create a script include (something like this):

var GroupVisibility = Class.create();
GroupVisibility.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVisibleGroups: function() {
        var userGroups = gs.getUser().getMyGroups(); // Get the groups the user belongs to
        var visibleGroups = [];
        var groupSysIdB = 'sys_id_of_group_b'; // Replace with the actual sys_id of group 'b'
        var groupSysIdA = 'sys_id_of_group_a'; // Replace with the actual sys_id of group 'a'

        // Check if user is a member of group 'b'
        if (userGroups.indexOf(groupSysIdB) !== -1) {
            // Add group 'a' and all other groups
            visibleGroups.push(groupSysIdA);
        }

        // Add all other groups except 'a'
        var gr = new GlideRecord('sys_user_group');
        gr.addQuery('sys_id', '!=', groupSysIdA);
        gr.query();
        while (gr.next()) {
            visibleGroups.push(gr.sys_id.toString());
        }

        return visibleGroups.join(',');
    },

    type: 'GroupVisibility'
});

and an advanced reference qualifier like this:

answer = 'sys_idIN' + new GroupVisibility().getVisibleGroups();

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark