From 97bdc8bc7cac7c13e63698da5e06ea8cfcf10600 Mon Sep 17 00:00:00 2001 From: chris evett Date: Mon, 24 Sep 2018 22:41:34 +0000 Subject: [PATCH 1/2] Adding custom helper for regex --- template/helpers.go | 6 ++++++ template/helpers_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/template/helpers.go b/template/helpers.go index 2c810fb..49e91ec 100644 --- a/template/helpers.go +++ b/template/helpers.go @@ -17,6 +17,7 @@ package template import ( "fmt" "net/url" + "regexp" "strings" "time" "unicode" @@ -117,3 +118,8 @@ func uppercaseFirst(s string) string { return s } + +func regexReplace(pattern string, input string, replacement string) string { + re := regexp.MustCompile(pattern) + return re.ReplaceAllString(input, replacement) +} diff --git a/template/helpers_test.go b/template/helpers_test.go index 7dd4cd0..686ee64 100644 --- a/template/helpers_test.go +++ b/template/helpers_test.go @@ -90,3 +90,11 @@ func TestUppercaseFirst(t *testing.T) { } } } + +func TestRegexReplace(t *testing.T) { + expected := "hello-my-String-123" + actual := regexReplace("(.*?)\\/(.*)", "hello/my-String-123", "$1-$2") + if actual != "hello-my-String-123" { + t.Errorf("error, expected %s, got %s", expected, actual) + } +} From 4071416038b84579ccd776314b3bdf6fe64cf734 Mon Sep 17 00:00:00 2001 From: chris evett Date: Tue, 25 Sep 2018 20:41:51 +0000 Subject: [PATCH 2/2] also register helper --- template/helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/template/helpers.go b/template/helpers.go index 49e91ec..8825b0e 100644 --- a/template/helpers.go +++ b/template/helpers.go @@ -40,6 +40,7 @@ var ( "lowercase": strings.ToLower, "trim": strings.TrimSpace, "title": strings.Title, + "regexReplace": regexReplace, } )