如何写正则式匹配css的引用链接?
例如下面这段js代码,怎么用正则从其中找出http://ads.sina.com/cnNews/css/cnNews.css这个链接?
browser = navigator.appName;
ie = "Microsoft Internet Explorer ";
netscape = "Netscape ";
os = navigator.platform;
mac = 'MacPPC '
if (browser == netscape && os != mac) {
document.write( ' <link rel= "stylesheet " type= "text/css " href= "http://ads.sina.com/cnNews/css/cnNews.css " title= "master "> ');
}
else if (browser == ie && os != mac) {
document.write( ' <link rel= "stylesheet " type= "text/css " href= "http://ads.sina.com/cnNews/css/cnNews.css " title= "master "> ');
}
else if (browser == netscape && os == mac) {
document.write( ' <link rel= "stylesheet " type= "text/css " href= "http://ads.sina.com/cnNews/css/cnNews.css " title= "master "> ');
}
else if (browser == ie && os == mac) {
document.write( ' <link rel= "stylesheet " type= "text/css " href= "http://ads.sina.com/cnNews/css/cnNews.css " title= "master "> ');
}
------解决方案--------------------试试
string pattern = @ "http://.*?\.css ";
Regex regex = new Regex(pattern, RegexOptions.Singleline);
foreach (Match m in regex.Matches(str))
{
MessageBox.Show(m.Value);
}
------解决方案--------------------string yourStr = ......;
MatchCollection mc = Regex.Matches(yourStr, " <link\\s+rel=\ "stylesheet\ "\\s+type=\ "text/css\ "\\s+href=\ "(? <url> .+?)\ ".+?> ", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
m.Groups[ "url "].Value;//http://ads.sina.com/cnNews/css/cnNews.css
}