首页 > 代码库 > 删除除了Src属性以后的全部属性

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

 

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

         return result;
     }

 

 

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

          Console.WriteLine (result);
      }

 

 

 

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

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