Mimic Positive Lookbehind in Javascript

JavaScript’s regular expression engine, while useful, lacks a few less frequently-used constructs.

I recently needed to remove all single whitespace characters (\s) immediately following semicolons (;) in a given block of text.  A simple way to accomplish this uses the positive lookbehind construct – that is, to match certain characters (;), but not to consume them, only to assert whether a match occurred or not.

This would normally be written as:

text.replace(/(?<=;)\s/g, '');

Though JavaScript doesn’t support lookbehinds, we can use a lambda expression to mimic its zero-width assertion behaviour:

text.replace(/(\;|:)?\s/g, function(str, p1) {
	return p1 ? p1 : str;
});

I haven’t tested this workaround exhaustively, but it solved the problem that needed to be overcome.

Update

It seems I’m not the first to have tackled this.  Steve Levithan’s regex skills far exceed my own, and his blog entry on the subject offers a more comprehensive solution to the problem.

2 Comments

  1. haemin said…

    Written on Sat, Oct 18, 2008 at 10:22pm · Link

    ok i don’t understand any of this, but just wanted to say hello and show some support! i’ll pass it on to my brother though… he appreciates this kind of geek-talk :)

  2. Alex said…

    Written on Tue, Aug 4, 2009 at 9:36am · Link

    Very interesting blog. We’ll go to him more often.

Comment RSS · TrackBack URI

Leave a Comment

Name

E-mail

Website (optional)

Comment