tags:

views:

58

answers:

2

Hi,

I am working on a web site with several other developers and we have had problems where people commit JavaScript code with AJAX calls that use URL's relative to the site root. An example would be /Home/Index which will not work if the site is hosted in a virtual directory.

To get round the problem we use a $.url() method to convert it to a full path, e.g.

$("#container").load($.url("/Home/Index"))

I am trying to write a unit test that will search each JavaScript file and find places where the $.url method is not being used. The only problem is that I cannot seem to write a regex expression to do this. I have tried the following:

(?!\$\.url\()"(/\w*)+"

But this does not work. I cannot find a way to say that I don't want the $.url in front. Does anyone know if this is possible? Note that I need regular expressions that are compatible with .NET

Thanks.

A: 

Why don't you just search for something like this?

\.load\("(/\w*)+"\)

Or something escapes me?

Janci
Hi, I may end up doing something like that. But the problem is that the load method is just one of several methods we are using that deal with URL's. For instance, there is also the $.ajax method. Each case would require a separate regular expression. There are also situations where URL's are being passed as variables and these would not be found. I was hoping for a generic solution if possible but it's turned out to be harder than I was expecting.
James
It's always trouble to match for something that is not there. I've tried this in PHP (I'm not a .NET guy), and it didn't work as well. It seems to simply ignore the do-not-match sub-pattern, and treat the regex like "(/\w*)+". The only workaround I'm able to think of at the moment is matching against (.{0,6})"(/\w*)+" and then checking if the first sub-pattern is equal to "$.url(". Hope that makes sense.
Janci
+2  A: 

You just need to change your lookahead to a lookbehind:

(?<!\$\.url\()"(/\w*)+"
Alan Moore
Thanks for the reply. I realised that my original regex was wrong: the asterisk after the 'w' have been a plus. After I fixed that, it seems to work quite well with your modifications. But it gets fooled by situations like this: document.location = $.url("/Report/" + $("#ddlObjectName").val() + "/New");Is there any easy way round this? If there is a way I could check for a plus then I could exclude this match but I'm not sure how to do it.
James
You're just trying to *locate* the offending entry, not match the whole thing, right? This should suffice: `(?<!\$\.url\()"/\w+`.
Alan Moore