Regular expressions in JS
by Alon Rotem
JavaScript
/*
This regex will capture style attributes in this html string, then extract the style value strings.
*/
var htmlStr =
"<h1 style=\"color:blue;text-align:center\">This is a header</h1>\
<p style=\"color:green\">This is a paragraph.</p>";
var regExPattern = /style\=[\'\"]([^\'^\"]*)[\'\"]/g
var regEx = new RegExp(regExPattern);
var matchedString = regEx.exec(htmlStr);
while (matchedString){
for(var i=0; i < matchedString.length; i++)
{
alert(matchedString[i]);
}
matchedString = regEx.exec(htmlStr);
}