zoukankan      html  css  js  c++  java
  • 正则匹配

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

    <a[^>]+href="([^"]*)"[^>]*>([sS]*?)</a>
    分组1和分组2即为href和value
    
    解释:
    <a                                        #匹配a标签的开始
    
    [^>]+                                      #匹配a标签中href之前的内容
    
    href="([^"]*)"                       #匹配href的值,并将匹配内容捕获到分组1当中
    
    [^>]*>                                    #匹配a标签中href之后的内容
    
    ([sS]*?)                                  #匹配a标签的value,并捕获到分组2当中,?表示懒惰匹配
    
    </a>                                       #匹配a标签的结束
    

     

    2.匹配所有图标

            public static string[] GetHtmlImageUrlList(string sHtmlText)
            {
                // 定义正则表达式用来匹配 img 标签 
                Regex regImg = new Regex(@"<img[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", 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 (!(/[wW]+/.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}$/

      

  • 相关阅读:
    zepto的源代码注释(转)
    关于js的连续赋值
    一道js题
    深入理解setTimeout的作用域
    深入理解setTimeout和setinterval
    webapp之路--apple私有属性apple-touch-icon
    javascript中的原型继承
    webapp之路--百度手机前端经验(转)
    (转)浏览器的渲染原理
    node.js study: cluster
  • 原文地址:https://www.cnblogs.com/0to9/p/7921976.html
Copyright © 2011-2022 走看看