Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

OK, this isn't really a SharePoint question, per se, but I need some regex help for my jQuery Library for SharePoint Web Services , which is certainly for SharePoint!

I need to be able to parse out the attributes in SCRIPT tags reliably for my SPScriptAudit function. jQuery does an excellent job on SCRIPT in the BODY but won't touch anything in the HEAD, so I need to do more heavy lifting to parse SCRIPTs there.

So for SCRIPT tags like these:

<script type="text/javascript" language="javascript" src="/_layouts/1033/init.js?rev=qX%2BG3yl4pldKy9KbPLXf9w%3D%3D"></script>
<script type="text/javascript" language="javascript" src="/_layouts/1033/core.js?rev=CNBZRdV1h3pKuA7LsMXf3w%3D%3D" defer></script>
<script type="text/javascript" language="javascript" src="/_layouts/1033/ie55up.js?rev=Ni7%2Fj2ZV%2FzCvd09XYSSWvA%3D%3D"></script>

I need to be able to get the values of language, type, src, etc. reliably. I'd appreciate help from any regex gurus out there. I think I've reached the point of diminishing returns trying to solve this.

share|improve this question
    
As you mention this is borderline SharePoint-related! I was too slow to pick this one up but please ask at somewhere like StackOverflow for this sort of thing next time. Thanks ;-) –  Alex Angas Jan 18 '10 at 1:52
    
Agreed. Oblique at best, and I'll try to honor the site intent better in the future. The good thing is, however, that I got some great help from Anders below. I think having the right SharePoint perspective probably allowed him to give me a better answer? (OK, still a stretch.) –  Marc D Anderson Jan 18 '10 at 3:46
add comment

1 Answer 1

up vote 4 down vote accepted

First off, i can recommend Expresso to build and test regular expressions.

Try this:

<script?\w+((\s+\w+(\s*=\s*(?:"(?<value>.*?)"|'(?<value>.*?)'|[^'">\s]+))?)+\s*|\s*)/?>*</script>

Some things to notice:

  • You should run it with Ignore Case, to capture capital script tags.
  • The named capture group "value" contains the value of the attribute
  • The expression takes hand of
    attribute values surrounded by
    both ",' and nothing (eg.
    key="value", key='value',
    key=value).

[edit: added rough javascript example]

Hi Marc, added an example of how to use the pattern in javascript. I simplified it a bit, so if you need check for both ",' and no surrounding quotes you need to stuff that in again. Also removed the check for last

Be aware this is boilerplate code that just show you how to get all hits from the attribute in question with the source to parse as parameter. I did not add any logic on how you would return anything (if you for example wants all (here 3) src attribute values, you could add them to an array and return them).

function getAttributeValue(attribute, source)
{
    var regex = RegExp("<script?\\w+(?:\\s+(?:" + attribute + "=\"([^\"]*)\")|[^\\s>]+|\\s+)*>","gi");
    var matches;
    while ( matches = regex.exec(source) )
    {
    	alert(matches[1]); 
    }
}

hth Anders Rask

[Edit: NB] OBS: Just for the record, i see several other ways to do this that might be better for your purpose (like DHTML/DOM). Regular Expressions are quite often seen as the silver bullet of string handling. I dont always agree that they are. Parsing HTML can be very tricky, especially because the rules (standards) often are bent (browsers are forgiving), and you can have nested tags, both attributes with double quotes and single quotes or even without quotes at all is legal (even within same element), attribute order is random, html comment tags can contain html elements. For those reasons parsing HTML with RegExp is often a poor choice (especially when we talk JavaScript thats lacking some of the smarter regex functionality like recursion that languages like Perl and .NET has).

share|improve this answer
    
Beautiful, Anders! Not only a great answer, but I get a cool new tool to add to my arsenal. Thanks! –  Marc D Anderson Jan 15 '10 at 20:59
    
Actually, I'm having a little trouble getting this to work in JavaScript/jQuery. As you undoubtedly know, the regex syntax is a little different in JavaScript and Expresso doesn't support it at the moment. I may get it sorted out... –  Marc D Anderson Jan 16 '10 at 4:17
    
Here's what I've got. I'm just not sure what I'm doing, to be honest. I'd like to be able to pass in the attribute name, e.g., "src" and get it's value (or null) back. function attrRegex(t) { var attrRegex = /<script?\w+((\s+\w+(\s*=\s*(?:"(.*?)"|'(.*?)'|[^'">\s]+))?)+\s*|\s*)\/?>*<\/scr‌​ipt>/gi; var av = attrRegex.exec(t); alert("STRING=::" + t); for(var i=0; i < av.length; i++) { alert(":AV[" + i + "]=::" + av[i] + "::"); } return av; } –  Marc D Anderson Jan 16 '10 at 4:48
    
You would probably want to either make the attribute part of the pattern (see example here fyneworks.blogspot.com/2007/04/…) or do a nested pattern match where you match on src="someval" and then do a submatch on that to get the value (see example here blog.strictly-software.com/2009/01/…). –  Anders Rask Jan 16 '10 at 13:42
    
Point taken on on using regex here at all. I think given the narrow scope of what I'm trying to do that it's a reasonable approach: I'm only looking to parse the SCRIPT tags in the HEAD, no more. Promise I won't make it a habit. ;+) –  Marc D Anderson Jan 18 '10 at 3:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.