Home & blog  /  Tag: REGEXP  /

My article in .NET / update to Dropdown

posted: 03 Sep '11 18:54 tags: REGEXP, .NET, article, me, dropdown, image

Remember I blogged a few months back that I'd been approached to write for .NET? Well my article is in the latest (September) issue, so perhaps you've seen it.

It's a beginner's guide to tackling regular expressions - one of the most avoided and feared areas of programming for elementary-intermediate-level developers.

There's an annoying typo; the * modifier, of course, matches zero or more characters, not one or more. D'oh. I had meant the + modifier, which does match one or more characters.

In other news, following a recent update to my jQuery Dropdown script (which allowed it to be used as a form field, not just as a navigational tool), there's been another update.

In the comments on the script page, Michael Eaton requested that it be possible to include icons in the 'options'. This is now possible!

Head over here to download, get usage info or view a demo.

post a comment

REGEX article by me in .NET magazine

posted: 13 Jul '11 21:07 tags: REGEXP, .NET, article, me

I've just completed an article for .NET magazine, to be printed in one of their upcoming issues. The article is an introduction to - what else - regular expressions, and why you should get into it.

There's three good reasons, actually. Firstly, and most simply, you'll gain the benefit of a very powerful part of programming. Secondly, REGEX grammar is implemented in much the same way across multiple programming languages, so learning it in Javascript also means learning it (or at least to a large degree) in whatever else you program in - say, PHP.

Lastly, and perhaps most importantly, it sets you apart. The age of jQuery means everyone is a JS developer these days. Mentioning on your CV that you know REGEX makes clear you REALLY know Javascript, rather than simply dabble with a few jQuery plugins.

So look out for that one. I'll do a follow-up post when they finally publish it.

2 comments | post new

Understanding multi-line mode for JS REGEX

posted: 30 Jun '11 21:37 tags: REGEXP, Javascript, look-behind, simulation

I wanted to do a post on regular expressions' multi-line mode, since from looking around the net there appears to be a common misconception about what this does.

That's kind of understandable; you might expect something called multi-line mode to enable your REGEX pattern to match within strings that contain line breaks.

But that happens anyway, with or without multi-line mode turned on. See:

1var myStr = "This is a \n multi-line \n\ string";

2words = myStr.match(/\w+/g);

3if (words) alert(words.join('\n'));

That will find and alert out all the words (notice I pass the global flag, as I want all the words, not just the first) - even though I ran the REGEX on a multi-line string and didn't stipulate multi-line mode.

No; what multi-line mode is about is changing the behaviour of the ^ and $ anchors.

Normally, these match the start and end of the string, respectively. In multi-line mode, though - which is turned on by passing an 'm' after the final forward slash of your pattern - their meanings are extended to also match the moments before ($) and after (^) a line-break.

So imagine I have a pattern which tries to match as many characters as it can, in succession, of any kind (that's what the [\s\S] does - matches all characters, including spacial characters). Let's try it without multi-line mode first:

1var myStr = "This is a \n multi-line \n\ string";

2alert(myStr.match(/^[\s\S]+$/g));

There, I simply get back the whole string. The ^ matches the start of the string, then matches all the characters in succession, then finally hits the end of the string ($). But in multi-line mode:

1var myStr = "This is a \n multi-line \n\ string";

2alert(myStr.match(/^[\s\S]+$/gm));

This time we get back an array of 3 items - one for each word or sequence of words delimited by the line breaks.

So what's happening there is the ^ matches the start of the string, it matches "this is a " but then finds the starting edge of a line-break. In multi-line mode, the $ matches this, so that's the end of a match. And since I'm in global mode, matching continues after the line-break.

So in conclusion, not the most indicative of names, but it can be useful. Not often, mind...

post a comment

Bizarre webkit bug with 'typeof'

posted: 01 Mar '11 08:28 tags: Javascript, Webkit, browser, bug, REGEXP

That'll teach me to post scripts and assume, without checking, that the normally-reliable Chrome and Safari will play ball. The other day I posted my JS simulation of REGEXP lookbehinds, but Marcus Tucker found it didn't work in these browsers.

After some digging, it seems there's a bizarre bug in Webkit browsers (i.e. Chrome and Safari) whereby regular expression objects are considered functions, not objects. So:

alert(typeof /\w+/);

Opera, IE and FF all say 'object', as you'd expect, but Chrome and Safari say 'function'. Very odd. I fixed the issue by using instanceof rather than typeof:

1alert(/\w+/ instanceof RegExp)

2//all browsers say true - happy days!

post a comment