Using javascript’s RegExp exec method

The exec method off the RegExp object in javascript can cause some confusion if you’re not used to it. Let’s say we had the following simple regex:

var regex = /the/i;

So we’re looking for the word “the” with the case-insensitive flag enabled so it will match “the”, “THE”, “The”, etc. Here’s some sample subject text:

var subject = 'The quick brown fox jumps over the lazy dog';

If we go with this “as is”, we’ll get something like the following:

var match = regex.exec(subject);
if (match) {
    alert(match[0]);
}

This will alert “The”, but what about the other instance of “the” in the sentence? How come that wasn’t matched? Oh! We forgot to set the global flag on the regex object. Now it should look like this:

var regex = /the/ig;

And the matching code now looks like:

var match = null;
while ( (match = regex.exec(subject)) ) {
    alert(match[0]);
}

Now we get both instances of “the” in the sentence. So what exactly is going on here? Let’s slightly change that last block of code to this:

var match = null;
while ( (match = regex.exec(subject)) ) {
    alert(match[0] + ' ' + regex.lastIndex);
}

So here, we see the regex object has a property called lastIndex that is set after every iteration of “exec()”. If you run the code, you’ll get something like “The 3″ and “the 34″. Once “exec()” finds a match, it sets the lastIndex property of the regex to the character right after the matched text. The next time it runs through the loop, it checks this lastIndex position and starts from there.

I’ll end this with a couple of cautionary warnings:

  • Don’t set the regex inside the loop. This would cause the lastIndex property to always be initialized to zero, hence, infinite loop if it was run against text with a match.
  • Modifying the subject string during the exec loop is dangerous and can also lead to an infinite loop.

Hope this helps some people!

jQuery Expected

Long time no write. Work has been crazy busy and life is pretty good. Here’s a quick plugin I wrote to help defensive programming when using jquery selectors.

jQuery Expected is inspired by .NET’s Enumerable.Single. It takes in an expected value and returns the collection if the number of items in the selector array matches the expected value. When the expected value doesn’t match, an Error of type ExpectedValueError is thrown. ExpectedValueError is included in the global namespace.

example:

try {
    $('selector').expected(1).html('hello world');
} catch (ex) {
    if (ex instanceof ExpectedValueError) {
        alert('Selector did not have expected length of 1');
    } else {
        console.log(ex);
    }
}

check it out here at github.

jQuery Listerine Plugin

I’ve been helping a friend with his site, www.thehardwareproject.org, for the past couple of months. It was a good chance to rework a lot of my php CMS and one thing I noticed was that there were a lot of pages where all I was doing was displaying information via a list of divs. Different pages might have the lists formatted differently, but rather than write page-specific backend code to format the lists, why not output all lists uniformly to the page and then have client-side code manage the display?

Listerine has 2 modes. One is “columns” which basically creates a user-set number of columns on the page and then evenly distributes the list items between columns. The other mode is “grid” which is more of a tile-like display.

simple columns example:

$('.manufacturer_list_container').listerine({
    cols: 3,
    transform: 'columns'
});

Download the code here.

Disable Script Debugging in Visual Web Developer 2010 Express

This little registry hack will disable javascript debugging in Visual Web Developer 2010 Express. You should really be using something like Chrome Dev Tools or Firebug when debugging javascript anyways.

reg add HKLM\SOFTWARE\Microsoft\VWDExpress\10.0\AD7Metrics\Engine\{F200A7E7-DEA5-11D0-B854-00A0244A1DE2} /v ProgramProvider /d {4FF9DEF4-8922-4D02-9379-3FFA64D1D639} /f

jquery-querystring

Just published a plugin that extends jquery to give access to querystring params easily. For the longest time, I was just copy/pasting one function into all my projects to do this, but finally decided to make it a jquery plugin for future use. Here’s how to use it:

$.querystring(param_name_here);

example:

var q = $.querystring('q');

Ezmode. get the code here: https://github.com/gehsekky/jquery-querystring

ShibalBot – A Python IRC Bot using the Twisted library

I used to use eggdrops back in the day when we would idle in IRC channels for counterstrike and used to write custom TCL scripts to do all that annoying IRC bot stuff (like listing scrim availabilities, cs clan rosters, etc). So while I’m learning Python, I figured I’d try and write my own bot to see if I could do it. I first started off with this example that builds the core of the bot in less than 30 lines of code. While that was fantastic, I then came across this example which uses the Twisted lib and makes it much more robust. I didn’t implement the markov chains, but I wrote up some quick code that can handle saving and displaying IRC quotes. This version of the bot is now just under 180 lines.

You can view the code here.

To use it, you have to change this section at the bottom of the file.

if __name__ == "__main__":
    reactor.connectTCP("YOUR_IRC_SERVER_HOST", 
                       IRC_SERVER_HOST_PORT,
                       ShibalBotFactory("YOUR_IRC_CHANNEL", 
                                        "YOUR_IRCBOT_NICK", 
                                        True, False))
    reactor.run()

Just change the parameters to your own IRC server address, server port, IRC channel, and bot nickname and you’re good to go. Some Todo’s include adding some code to fetch the titles of url’s pasted in the channel, making it modular so you can just drop in scripts instead of having to change the main file, and updating the quotes module to not load the entire quotes file at once (in case the file becomes huge).

Script to sync friends list across multiple reddit accounts

While teaching myself Python, I wanted to try and write something that I would actually use. Since I have multiple reddit accounts, a script that would sync settings across them seemed like it would be useful. So far, the only thing being synced is friends, but I’ll soon have something that also syncs subscribed subreddits as well (hopefully).

The script is being hosted on github and the link is here.

I developed the script using Python 2.7.2 so I don’t even know if it is compatible with Python 3.x. Using the script is fairly easy. Just edit the top of the file above the line that says “DO NOT MODIFY BELOW THIS LINE”.

Example:

login_info = [
["user1", "pass1"],
["user2", "pass2"],
["user3", "pass3"]
]

add_self_to_friends should be either "True" or "False"
(without quotations)

I think I’ll add in some logic to do interactive input if no settings are detected in the file. I’m also going to change the script to do dom parsing as opposed to regex because the regex could run into some weird issues (I know I’ve said dom parsing is a hassle before, but the goal here is to learn as much Python as possible so I’ll try attacking problems from all angles to learn everything).

mentally mutilated

“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”

-Edsger W.Dijkstra

This quote ran through my head today after I realized (after 5 minutes of head scratching) why this line was throwing an exception in javascript:

[javascript]var sHeading = oInput(0);[/javascript]

Parenthesis for array indexing? Damn you VB!