Home & blog  /  2011  /  Feb  /

Simulating REGEX look-behinds in JavaScript

posted: 27 Feb '11 12:48 tags: REGEXP, Javascript, look-behind, simulation

It's no secret that JavaScript's implementation of regular expressions is pretty basic compared to, say, that of PHP. Even then, the lack of suport for look behind assertions (LBAs) is massively frustrtaing.

So I wrote my own workaround. Head over here to download, get usage info or view a demo.

My simulation defines three methods: match2 and replace2 (of the String object), and test2 (of the RegExp object). All work like their native counterparts, except they each accept an additional parameter - the LBA (as a string).

So to change all scary animals to scary monsters, you could do this:

1alert('scary lion; scary crocodile; cute puppy'.replace2(/[a-z]+/gi, 'monster', '(?<=scary )'));

2//== scary monster; scary monster; cute puppy

You see my current project involves writing a script which localises British into US English.

Some words change only when used as nouns. For example 'torch' becomes 'flashlight', but only when used as a noun. So it would change in the sentence "to shine a torch", but not in "to torch a building". 'Film' > 'movie' is another such example.

I realised the way to detect the role of a word within a sentence was (at least chiefly) to look at the word preceding it. For example if the word was preceded by the infinitive preposition 'to', or by a pronoun, it was most likely a verb.

Then I remembered you can't do LBAs in JavaScript, and I spent days pulling my hear out.

I hope it proves as useful to some of you as it has to me! Head over here to download, get usage info or view a demo.

post a comment

Smashing jQuery out now (I was technical editor...)

posted: 19 Feb '11 12:36 tags: book, Wiley, smashingmagazine, jQuery

Following the article I wrote for Smashing Magazine last year, I was approached by their publishers, Wiley, to act as technical editor for their forthcoming book, Smashing jQuery.

This has just been published, and is .

It's aimed at beginners to intermediate jQuery users and covers pretty much everything, from the basics - selectors, chaining etc - to more intermediate topics such as event handling and AJAX.

Speaking of Smashing magazine, look out for another article of mine shortly to be published on their website. It will be a tutorial on building an image manipulation tool using a mixture of jQuery and PHP.

post a comment

Bouncy text plugin

posted: 03 Feb '11 16:42 tags: text, effect, animation, bounce, jQuery

Some months ago I posted my Spraycan plugin. Just when you thought I couldn't post anything more pointless, I give you my bouncy text plugin.

This, erm, does what it says on the tin. You point it at some text on your page via jQuery selector, and it randomly makes the letters in that element bounce up and down.

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

It works in all IEs, Firefoxes and Operas, but currently not Chrome or Safari. How about that? Something works in IE6 but not Chrome. Rather amusing.

I will try to get out more.

post a comment
older posts >