正则匹配

1.正则匹配所有的a标签

<a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)</a>
分组1和分组2即为href和value

解释:
<a\b                                        #匹配a标签的开始

[^>]+                                      #匹配a标签中href之前的内容

\bhref="([^"]*)"                       #匹配href的值,并将匹配内容捕获到分组1当中

[^>]*>                                    #匹配a标签中href之后的内容

([\s\S]*?)                                  #匹配a标签的value,并捕获到分组2当中,?表示懒惰匹配

</a>                                       #匹配a标签的结束

 

2.匹配所有图标

        public static string[] GetHtmlImageUrlList(string sHtmlText)
        {
            // 定义正则表达式用来匹配 img 标签 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            // 搜索匹配的字符串 
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            string[] sUrlList = new string[matches.Count];
            // 取得匹配项列表 
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;
            return sUrlList;
        }

  

3.匹配号码和名字

if (!(/[\w\W]+/.test(form.find("[name=name]").val()))) {
            alert("请输入姓名");
            form.find("[name=name]").focus()
            return false;
        }
匹配号码:
校验2位 : /^1[3|4|5|7|8][0-9]{9}$/
校验第一位 :
/^1[0-9]{10}$/

  

posted @ 2017-11-29 18:02  少时不知贵  阅读(199)  评论(0编辑  收藏  举报