删除除了Src属性以后的全部属性

 

public static string RemoveAllAttributesWithoutSrc(string input)
     {
        
         string pattern = @"<([a-z][a-z0-9]*)(?:[^>]*(\ssrc=['\""][^'\""]*['\""]))?[^>]*?(\/?)>";
         var result = Regex.Replace(input, pattern, "<$1$2$3>");

         return result;
     }

 

 

[TestMethod]
      public void TestMethod1()
      {
          var input = @" <a style='asdfasdfasdfasdfasdf' class=""afasdfasdfasdfasdf"" /> <img src='www.abc.com/jpg/dd' />";
          string pattern = @"<([a-z][a-z0-9]*)(?:[^>]*(\ssrc=['\""][^'\""]*['\""]))?[^>]*?(\/?)>";
          var result = Regex.Replace(input, pattern, "<$1$2$3>");

          Console.WriteLine (result);
      }

 

 

 

PHP:http://stackoverflow.com/questions/2994448/regex-strip-html-attributes-except-src

posted @ 2016-08-30 12:50  张保维  阅读(213)  评论(0编辑  收藏  举报