首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么写正则式匹配css的引用链接

2011-12-24 
如何写正则式匹配css的引用链接?例如下面这段js代码,怎么用正则从其中找出http://ads.sina.com/cnNews/css

如何写正则式匹配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
}

热点排行