diff --git a/JexlNet.ExtendedGrammar/ExtendedGrammar.cs b/JexlNet.ExtendedGrammar/ExtendedGrammar.cs index d74fdd3..8ad4e3e 100644 --- a/JexlNet.ExtendedGrammar/ExtendedGrammar.cs +++ b/JexlNet.ExtendedGrammar/ExtendedGrammar.cs @@ -594,11 +594,11 @@ public static JsonNode Join(JsonNode input, JsonNode separator) /// A string with all occurrences of old replaced by new public static JsonNode Replace(JsonNode input, JsonNode old, JsonNode newStr) { - if (input is JsonValue value && old is JsonValue oldValue && newStr is JsonValue newValue) + if (input is JsonValue value && old is JsonValue oldValue) { string str = value.ToString(); string oldStr = oldValue.ToString(); - string newStrStr = newValue.ToString(); + string newStrStr = newStr is JsonValue newValue ? newValue.ToString() : ""; return str.Replace(oldStr, newStrStr); } return null; diff --git a/JexlNet.Test/ExtendedGrammar.cs b/JexlNet.Test/ExtendedGrammar.cs index b225f21..dbe14af 100644 --- a/JexlNet.Test/ExtendedGrammar.cs +++ b/JexlNet.Test/ExtendedGrammar.cs @@ -106,6 +106,17 @@ public void Contains(string expression, bool expected) Assert.Equal(expected, result); } + [Theory] + [InlineData("replace('foo-bar', '-', '_')", "foo_bar")] + [InlineData("replace('foo-bar----', '-', '')", "foobar")] + [InlineData("'123ab123ab123ab'|replace('123')", "ababab")] + public void Replace(string expression, string expected) + { + var jexl = new Jexl(new ExtendedGrammar()); + var result = jexl.Eval(expression); + Assert.Equal(expected, result?.ToString()); + } + [Fact] public void Split() {