JavaScript program to count lines of Java code
Sorry, don't really have the time to do a comprehensive review (maybe later I'll edit!) but for now:

Your code actually fails on some cases.

The way that you're finding whether a line counts as a LOC or not is too greedy. (Alternatively, your comment regexes are too strict.) You're reading in basically the whole line as a line of code. This fails for relatively simple cases:

a/*
b*/


should be one line of code, but you treat it as two, since your code regex absorbs the /*. In fact, I'd argue that the way that you choose to count lines of code is odd; it took me a few reads to see what you were doing and find that error. The more natural way perhaps would be to strip spacing and comments, then count non-empty lines.

That also has the advantage of being way faster: you're calling String#substr() a lot, which is pretty expensive.

Aside from that, I only have a few comments:


Keep spacing consistent. Choose either function() or function (); don't use both.
Be consistent in your choice of indentation character. This is easier if you enable showing whitespace in your editor.
Be consistent in string delimiters; don't switch between ' and " without a good reason.
Since you're using a bare object, you can iterate over it with a for..in. If you're really concerned that somebody will put properties into the prototype of Object (which, by the way, should NEVER EVER happen), then add the hasOwnProperty check:


    for (var tokenType in patterns) {
    //  if (patterns.hasOwnProperty(tokenType)) ...
        if (m = source.match(patterns[tokenType])) {
            var token = m[0];
            source = source.substr(token.length);
            //DEBUG
            console.log('Found token (%s): %s', type, JSON.stringify(token));
            return type;
        }
    }
    throw new Error('Unknown code pattern!');