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