正则取出某class或id的嵌套标签内容

前端之家收集整理的这篇文章主要介绍了正则取出某class或id的嵌套标签内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

正则取出某class或id的嵌套标签内容,自己都在用

/// <summary>
        /// 正则取出content里面class=classORidvalue,或id=classORidvalue的值
        /// 大小写,是否含引号都可以匹配出来
        /// 需要转义字符的要处理
        /// 调用例子GetTagExpress("div","id","110","<div id='110'>***********</div>")
        /// </summary>
        /// <param name="htmltag">标签名,如div,p</param>
        /// <param name="classorid">输入"class"或"id"</param>
        /// <param name="classORidvalue">class或id的值</param>
        /// <returns></returns>
        public string GetTagExpress(string htmltag,string classorid,string classORidvalue)
        {
            //<(?<ul>[\w]+)[^>]*\s[cC][lL][aA][sS][sS]=(?<Quote>["']?)productgroup-list(?(Quote)\k<Quote>)["']?[^>]*>((?<Nested><\k<ul>[^>]*>)|</\k<ul>>(?<-Nested>)|.*?)*</\k<ul>>
            string reclassORid = "[cC][lL][aA][sS][sS]";
            if (classorid.ToLower() == "id")
            {
                reclassORid = "[iI][dD]";
            }
            return string.Format("<(?<{0}>[\\w]+)[^>]*\\s{1}=(?<Quote>[\"']?){2}(?(Quote)\\k<Quote>)[\"']?[^>]*>((?<Nested><\\k<{0}>[^>]*>)|</\\k<{0}>>(?<-Nested>)|.*?)*</\\k<{0}>>",htmltag,reclassORid,classORidvalue);
        }
原文链接:https://www.f2er.com/regex/361594.html

猜你在找的正则表达式相关文章