曹永思

导航

实用的正则表达式集合总结

匹配纯汉字且只能输入 n到m个字区间

var nickname = value;
var regex = new RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]){n,m}$"); 
var res = regex.test(nickname);

匹配纯[汉字]或[字母]或[数字]或[_](下划线)且输入 最少n位

var nickname = value;
var regex = new RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_]){n,}$"); 
var res = regex.test(nickname);

匹配手机号码

var mobile= value;
var regex = new RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]){n,m}$"); 
var regMobile = /^(13|14|15|18)\d{9}$/;
var res = regex.test(mobile);

 判断字符串里包含数字的个数

  var arr1 = car.split('');
  var re2 = /^[0-9]$/;
  var count1 = 0;//数字的个数
  for (var i = 0; i < arr1.length; i++) {
    if (re2.test(arr1[i])) {
       count1 = count1 + 1;
      }
      else {
           }
   }

 获取文章中纯文字.net正则

   private string StripHT(string strHtml)  //从html中提取纯文本
        {
            Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);
            string strOutput = regex.Replace(strHtml, "");//替换掉"<"和">"之间的内容
            strOutput = strOutput.Replace("<", "");
            strOutput = strOutput.Replace(">", "");
            strOutput = strOutput.Replace("&nbsp;", "");
            return strOutput;
        }

获取文章中的图片.net正则

     /// <summary>
        /// 获取Img标签
        /// </summary>
        /// <param name="htmlText">截获到的html代码</param>
        /// <returns></returns>
        public static string GetPadImg(string htmlText)
        {
            List<string> list = new List<string>();
            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 mc = regImg.Matches(htmlText);
            foreach (Match m in mc)
            {
                return m.Groups["imgUrl"].Value;
            }
            return "";
        }

 

posted on 2015-05-22 11:10  曹永思  阅读(491)  评论(0编辑  收藏  举报