var strHref = top.document.location.href;
var regu = "OriginalPic";
var re = new RegExp(regu);
if (strHref.search(re) != -1) {
“have”;
}
var regu = "OriginalPic";
var re = new RegExp(regu);
if (strHref.search(re) != -1) {
“have”;
}
以上是JS的包含正则 ,search 确定一个字符串是否包含另外一段字符
------------------------------------------------------------------
C#的正则表达式
代码
html = "5800XM报价5480</a> <span>2008-11-11 </span></li><li><i>[<a href=\"http://yn.it168.com\">云南</a>]</i> <a title=\"[昆明]昆明十一月份最受关注十大手机\" href=\"http://publish.it168.com/2008/1130/20081130005201.shtml\">[昆明]";
string zhengze = "<div class=\"tit9\">(.+?)</div>";
Match m = Regex.Match(html, zhengze); //正则匹配
if (!string.IsNullOrEmpty(m.Groups[1].Value)){}
string zhengze = "<div class=\"tit9\">(.+?)</div>";
Match m = Regex.Match(html, zhengze); //正则匹配
if (!string.IsNullOrEmpty(m.Groups[1].Value)){}
去图片的正则也可以简化一下 <img\s[^<>]*?>
过滤所有HTML标签
代码
html = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", "", RegexOptions.IgnoreCase);
html = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", "", RegexOptions.IgnoreCase);
return html;
html = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", "", RegexOptions.IgnoreCase);
return html;
过滤所有换行
代码
/// <summary>
/// 别换行
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
private static string NonrBr(string html)
{
html = Regex.Replace(html, @"[\n\r]", "");
html = html.TrimEnd((char[])"\n\r".ToCharArray());
return html;
}
/// 别换行
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
private static string NonrBr(string html)
{
html = Regex.Replace(html, @"[\n\r]", "");
html = html.TrimEnd((char[])"\n\r".ToCharArray());
return html;
}
还有一些常用的处理
代码
private static void regexReplaceFilter(ref string answerStr)
{
MatchCollection mc = null;
string zhengZe = "";
//去链接
zhengZe = "<a[^>]*?href=[^>]*?>";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去图片
zhengZe = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去网址
zhengZe = @"http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>])*";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去QQ
zhengZe = @"[1-9]\d{4,8}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去电话
zhengZe = @"((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去手机
zhengZe = @"((\(\d{3}\))|(\d{3}\-))?13\d{9}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
}
{
MatchCollection mc = null;
string zhengZe = "";
//去链接
zhengZe = "<a[^>]*?href=[^>]*?>";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去图片
zhengZe = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去网址
zhengZe = @"http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>])*";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去QQ
zhengZe = @"[1-9]\d{4,8}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去电话
zhengZe = @"((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
//去手机
zhengZe = @"((\(\d{3}\))|(\d{3}\-))?13\d{9}";
mc = Regex.Matches(answerStr, zhengZe);
foreach (Match var in mc)
{
answerStr = answerStr.Replace(var.Value, "");
}
}