下面的代码用于修改html文本中的img标记,修改后的html适用于lazyload方式的图片加载:
- protected string LazyPicProcess(string content) {
- Regex re = new Regex("<\\s*img[\\s\\S]*?(?<src>src[\\s]*=[\\s]*[\"|\'](?<pic>[\\s\\S]*?)[\"|\'])[\\s\\S]*?>", RegexOptions. IgnoreCase);
- string str = re.Replace(content, new MatchEvaluator(ImgSrcReplace));
- return str;
- }
- public static string ImgSrcReplace(Match match) {
- string newtext = string.Empty;
- if (match.Groups.Count > ) {
- string img = match.Groups[].Value;
- string src = match.Groups["src"].Value;
- string pic = match.Groups["pic"].Value;
- newtext = img.Replace(src, "src=\"images/grey.gif\" data-img=\"" + pic + "\" isload=\"false\"");
- return newtext;
- }
- else {
- return match.Groups[].Value;
- }
}
关于延迟加载图片参考博文:
以上就是用正则表达式替换html标签的详细内容,更多关于用正则表达式替换html标签的资料请关注九品源码其它相关文章!