First off, i can recommend Expresso to build and test regular expressions.
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.
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");
while ( matches = regex.exec(source) )