diff --git a/source/CsQuery.Tests/CQ_CsQuery/AttrReplace.cs b/source/CsQuery.Tests/CQ_CsQuery/AttrReplace.cs
new file mode 100644
index 00000000..75b5855c
--- /dev/null
+++ b/source/CsQuery.Tests/CQ_CsQuery/AttrReplace.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using NUnit.Framework;
+using Assert = NUnit.Framework.Assert;
+using CsQuery;
+
+
+namespace CsQuery.Tests.CQ_CsQuery
+{
+ [TestClass,TestFixture]
+ public class AttrReplace : CsQueryTest
+ {
+
+ [TestMethod, Test]
+ public void Substring()
+ {
+ var expected = CQ.Create("
");
+ var cq = CQ.Create("");
+
+ Assert.AreEqual(expected, cq.AttrReplace("class", "one", "two"));
+ }
+
+ [TestMethod, Test]
+ public void Regex() {
+ var expected = CQ.Create("");
+ var cq = CQ.Create("");
+
+ Assert.AreEqual(expected, cq.AttrRegexReplace("style", @"font-size:\s+\d+px;?", ""));
+ }
+
+ [TestMethod, Test]
+ public void Regex_Evaluated() {
+ var expected = CQ.Create("");
+ var cq = CQ.Create("");
+
+ Assert.AreEqual(expected, cq.AttrRegexReplace("style", @"font-size:\s+(\d+)px;?", (m) => {
+ return m.Captures[0].Value.Replace(m.Groups[1].Value, "20");
+ }));
+ }
+ }
+}
diff --git a/source/CsQuery.Tests/Csquery.Tests.csproj b/source/CsQuery.Tests/Csquery.Tests.csproj
index 4ac4b573..19522b94 100644
--- a/source/CsQuery.Tests/Csquery.Tests.csproj
+++ b/source/CsQuery.Tests/Csquery.Tests.csproj
@@ -115,6 +115,7 @@
+
diff --git a/source/CsQuery/CQ_CsQuery/AttrReplace.cs b/source/CsQuery/CQ_CsQuery/AttrReplace.cs
index 2e5f9f8d..ba1bc352 100644
--- a/source/CsQuery/CQ_CsQuery/AttrReplace.cs
+++ b/source/CsQuery/CQ_CsQuery/AttrReplace.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using CsQuery.Utility;
using CsQuery.ExtensionMethods;
using CsQuery.ExtensionMethods.Internal;
@@ -44,5 +45,63 @@ public CQ AttrReplace(string name, string replaceWhat, string replaceWith)
}
return this;
}
+
+ ///
+ /// Perform a regex replace on the contents of the named attribute in each item in the
+ /// selection set.
+ ///
+ ///
+ ///
+ /// The attribute name.
+ ///
+ ///
+ /// The regex pattern.
+ ///
+ ///
+ /// The value to replace each occurrence with.
+ ///
+ ///
+ ///
+ /// The current CQ object.
+ ///
+
+ public CQ AttrRegexReplace(string name, string pattern, string replaceWith) {
+ foreach (IDomElement item in SelectionSet) {
+ string val = item[name];
+ if (val != null) {
+ item[name] = Regex.Replace(val, pattern, replaceWith);
+ }
+ }
+ return this;
+ }
+
+ ///
+ /// Perform a regex replace on the contents of the named attribute in each item in the
+ /// selection set.
+ ///
+ ///
+ ///
+ /// The attribute name.
+ ///
+ ///
+ /// The regex pattern.
+ ///
+ ///
+ /// The evaluator used to replace the pattern
+ ///
+ ///
+ ///
+ /// The current CQ object.
+ ///
+
+ public CQ AttrRegexReplace(string name, string pattern, MatchEvaluator matchEvaluator) {
+ foreach (IDomElement item in SelectionSet) {
+ string val = item[name];
+ if (val != null) {
+ item[name] = Regex.Replace(val, pattern, matchEvaluator);
+ }
+ }
+ return this;
+ }
}
}