Regex to Parse Script Tags

Marc D Anderson
  • Regex to Parse Script Tags Marc D Anderson

    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.

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

    Try this:


    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',

    [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) )

    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).

regex jquery javascript
Related questions and answers
  • ? &lt;link type="text/css" href="_layouts/test/themes/base/ui.all.css" rel="stylesheet" /&gt; &lt;script type="text/javascript" src="_layouts/test/jquery-1.3.2.js"&gt;&lt;/script>; &lt;script type="text/javascript" src="_layouts/test/ui/ui.core.js"&gt;&lt;/script>; &lt;script type="text/javascript" src="_layouts/test/ui/ui.tabs.js"&gt;&lt;/script>; &lt;link type="text/css" href="_layouts/test/demos.css" rel="stylesheet" /&gt; &lt;script type="text/javascript"&gt; $(function() { $("#tabs").tabs(); }); &lt;/script>;

  • . Any ideas how to implement? My major confusion is how to deal with the code in the head part of the following code? Any reference code or document? Here is the aspx code I am using, &lt;!doctype html> &lt;html lang="en"&gt; &lt;head> &lt;title>Test</title> &lt;link type="text/css" href="tabcontrol/themes/base/ui.all.css" rel="stylesheet" /&gt; &lt;script type="text/javascript" src="tabcontrol/jquery-1.3.2.js"&gt;&lt;/script>; &lt;script type="text/javascript"&gt; $(function() { $("#tabs").tabs(); }); &lt;/script>; &lt;/head>

  • :EcmaScriptEncode($ListName),'};'))}"&gt;&lt;/input>&lt;/td> I need to add javascript within the DataFormWebPart so I can update another form field when the user clicks the 'Complete Task' button. I...Hello I really need help with this... , I am using Sharepoint 2007. I have the 'collect data from user' action in a workflow - which generates a custom form within the workflow folder. The form...%"&gt; &lt;input type="button" value="Cancel" name="btnCancel" onclick="javascript: {ddwrt:GenFireServerEvent(concat('__cancel;__redirectsource;__redirectToList={',ddwrt:EcmaScriptEncode

  • (the code is very simple, just redirect to another page, and I write inline script code in, here is my code, and I met with unauthorized error (403) when I select to browse the page from IIS 6.0 manager. I am not in Active Directory or Windows domain environment. I have enabled permission to anonymous users for the whole SharePoint site. Any ideas what is wrong? &lt;%@ Page Language="C#" AutoEventWireup="true" %&gt; &lt;script runat="server" type="text/C#"&gt; protected void Page_Load(object sender,EventArgs e) { Response.Redirect(""); } &lt;/script>; &lt

  • to develop a WebPart, which refers css and javascript (.js) files. My question is (1) how to deploy css/javascript files and (2) how to write reference path (e.g. ../../themes from my code below) from webpart to refer to related css/javascript files? BTW: the existing code of css/javascript/Webpart is from existing aspx code and I am migrating aspx code to a Webpart. The code works in aspx. Currently my code looks like this, &lt;link type="text/css" href="../../themes/test.css" rel="stylesheet" /&gt; &lt;script type="text/javascript" src="../../test.js"&gt;&lt;/script>;

  • ;script language="javascript"&gt; $(document).ready(function() { $("#dialog").dialog({ bgiframe: true, height: 300, modal: true, closeOnEscape: false, width: 400, buttons: { 'Skip': function...','')} } }); }); &lt;/script>;&lt;div id="dialog" title="Question" style="display:none"&gt;&lt;span>&lt;/span>&lt;span>What is the Capital of Massachusetts ?&lt;/span>&lt;table id..." type="radio" name="ctl00$m$g_583145bb_3463_40cc_b5ef_6f42c371f376$ctl02" value="1" /&gt;&lt;label for="ctl00_m_g_583145bb_3463_40cc_b5ef_6f42c371f376_ctl02_0"&gt;Boston</label>&lt;/td>

  • ;/RenderPattern> In WSS 3.0, this would render the 2nd column in the field as a hyperlink in the LVWP, which is what I want. In SharePoint 2010, I tried adding TRUE to my field type definition, but that ends up rendering the actual anchor tag, as opposed to a clickable hyperlink: &lt;a href=""&gt;;/a> So then I tried creating a XSL transform, since...Has anybody upgraded an old custom field type to use the new XSL transform in SharePoint 2010? I have a multi-column field type, and want the Display pattern to render the 2nd "column", which

  • it always shows me the LINK to the item in the reusable content list - what I want it to show is the ReusableHTML Column of the specific item in the list. I tried doing so with &lt;property name="DataColumnRenames" type="string"&gt;ReusableHtml,Reuse</property> &lt;property name="CommonViewFields" type="string"&gt;ReusableHtml,Text</property> But somehow the CQWP doesn't display anything when using these two properties. My reusable content list basically looks like so: Title ReusableHTML Static 1 &lt;a href="#"&gt;Link 1&lt;/a> Static 2 &lt;a href="#"&gt;Link 2&lt;/a>

  • , Status) { $(divId).html(""); } }); But is it also possible to add a Where like : &lt;Where>&lt;Eq>&lt;FieldRef Name="Status"/&gt;&lt;Value Type="Text"&gt;Ready</Value>&lt;/Eq>&lt;/Where> Or should I do it like this post ? (First retrieve the list and then do a loop for each item ?) ...I've searched the jQuery SPServices code examples and MSDN references for an simple example how to update several ListItems using a Where-Clause. Code for updating a single ListItem with ID = 500

Data information