Search code examples
javascriptcomma-operator

Comma operator in return


I was reading the JS from one page and this is what I found (the source is deobfuscated by google chrome dev tools):

var db = function(a) {
    return a.replace(/[^\w\s\.\|`]/g, 
    function(b) {
        return "\\" + b
    })
};

Is there a some trick with the first comma operator operand (the a.replace() one)?

From my point of view the a.replace(/[^\w\s\.\|``]/g, part is completely redundant and can be removed.

Have I missed something?


Solution

  • It is not the comma operator, but a simple arguments list of the call to .replace - notice the parenthesis.

    Your deobfuscator better should've indented it like this:

    return a.replace(/[^\w\s\.\|`]/g, function(b) {
        return "\\" + b;
    });
    

    Btw, that function could be replaced by the simple string "\\$&".