删除除了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

原文地址:https://www.cnblogs.com/zbw911/p/5821560.html