为了账号安全,请及时绑定邮箱和手机立即绑定

C# 写个正则匹配html中的JS文件

C# 写个正则匹配html中的JS文件

C#
慕勒3428872 2018-07-28 09:08:51
.求写个正则,十分感谢。如何获取JS脚本文件的路径
查看完整描述

2 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

 List<string> GetJsFiles(string htmlPath)
        {
            List<string> jsFiles = new List<string>();            string spacePattern = @"\s*";            string srcPattern = string.Format("src{0}={0}\"(?<srcPath>[^\"]*)\"", spacePattern);            string scriptPattern = string.Format(@"\<script[^\>]*{0}[^\>]*\>", srcPattern);            string htmlContent = File.ReadAllText(htmlPath);
            MatchCollection ms = Regex.Matches(htmlContent, scriptPattern, RegexOptions.IgnoreCase);            if (ms.Count > 0)
            {                for (int i = 0; i < ms.Count; i++)
                {                    if (ms[i].Success)
                    {                        string srcPath = ms[i].Groups["srcPath"].Value;
                        jsFiles.Add(srcPath);
                    }
                }
            }            return jsFiles;
        }


查看完整回答
反对 回复 2018-07-30
  • 2 回答
  • 0 关注
  • 893 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信