views:

60

answers:

3

Hey,

Say you have the following in files:

<script src="../../Public/Javascript/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Public/Javascript/jquery.easing-sooper.js" type="text/javascript"></script>
<script src="../../Public/Javascript/jquery.easing.min.js" type="text/javascript"></script>

I'm looking for a regex to find the First line (not minified and doesn't contain the word sooper). I would be using this in Visual Studio as an FYI.

Sorry, I need to be more specific. I would be using this in a Visual Studio find, as in control + shift + F, then click the use regular expressions for the find, to find in the project where I have javascript files that are referenced that are not minified. I need the exception b/c some are ok to not be minified.

I'm looking for the lines|matches that:
-have .js somewhere in them
-but not .min.js
-AND Not the word sooper

Thanks again!

I would greatly appreciate it!

A: 
<script src="(?:(?!\.min\.|\bsooper\b)[^"])+" type="text/javascript"></script>

will match the first line but not the other two.

I hope I guessed correctly what your requirements are. This regex will match a script tag with any src attribute as long as this attribute doesn't contain either the string .min. or sooper.

Be aware that using regexes to match HTML and other non-regular languages is brittle at best and leads to madness at worst.

Tim Pietzcker
Thanks Tim. For some reason, when putting the reg ex in Visual Studios find, it complains, Grouped expression is missing ')'. I'm battling with the format it wants it in. I appreciate your help.
Neo302
Hm, strange. The parentheses *are* balanced... Does it help to change the `(?:` to `(`?
Tim Pietzcker
I just tried it with Visual Studio. The error goes away when changing the non-capturing group to a capturing group, but the regex doesn't match anything. Apparently Visual Studio doesn't support lookahead in regular expressions. I think this problem needs lookahead if it is to be solved in a single regex, though. Sorry.
Tim Pietzcker
A: 

You aren't specifying your regex engine.

In my opinion what would work best is using a capturing group for the part in the filename that specifies the jquery version:

jquery([a-zA-Z0-9-.]+).js 

group one in this will yield the part you need to discriminate for.

Now all there is to do is check that string for either min or sooper substrings.

FK82
+1  A: 

Try this:

\<script src="(~(<(min|sooper)>)[^"])+" type="text/javascript"\>\</script\>

The VS regex flavor has only a passing resemblance to the Perl-derived flavors most of us are used to. Notably, negative lookahead is ~(...), and < and > are word boundaries (start and end), and you have to escape them to match literal angle brackets.

EDIT: This version looks specifically for filenames ending with .js, but not containing min.js.

\<script src="(~(<(min\.js|sooper)>)[^"])+\.js" type="text/javascript"\>\</script\>
Alan Moore
Hey Alan, This is matching everything with brackets. I'm doing a find in Visual Studio with regular expressions enabled. The reg ex doesn't literally go inside the script tag. I'm looking for the lines|matches that: -have .js somewhere in them -but not .min.js -AND not the word sooper so I can make sure the files I want are minified.The reg ex you provided returns all lines with brackets in them, as well, not only lines with .js in them. Thank you for your help!
Neo302
I don't know what you mean by "everything with brackets" but I edited the regex to match only if the `src` attribute ends with `.js` (but not `min.js`). Does that work for you?
Alan Moore
Hey Alan, This is close, but it returns everything with .min.js or sooper, and that has .js. I want it to have .js, but not .min.js and not sooper. Thanks again!
Neo302
I don't know what to tell you; it works fine when I test it in Visual C# 2008 Express Edition.
Alan Moore
Hey Alan, this is actually awesome and I'm marking as answer. Is it possible for it to work with a less strict format? I was trying to get it to work by only using the reg ex, leaving out the explicit script, type and src tag. Starting and ending with script is fine, but whats in between being less strict would be great. Thanks again for your great help!
Neo302
Actually, I think I got it Alan. Thanks again. You da man!
Neo302
thumbs up for knowing a capricious regex flavour :)
FK82