下面是群里一个同事C#写法 大致相同这个是带当前标签的,贴下,记录给需要的同学:
var pattern = "<\\S[^><]* class=[\"|'](.*?)[\"|'].*?>"; var classFormat = "class=\"{0}\""; var regex = new Regex(pattern); if (!string.IsNullOrEmpty(Prefix)) { Match m = regex.Match(source); while (m.Success) { Group group = m.Groups[1]; var oldClass = group.Value; var newClass = string.Join(" ", oldClass.Split(' ').Select(o => Prefix + o)); source = source.Replace(string.Format(classFormat, oldClass), string.Format(classFormat,newClass)); m = m.NextMatch(); } }