Adding custom helper for regex

This commit is contained in:
chris evett 2018-09-24 22:41:34 +00:00
parent 5748d3149f
commit 97bdc8bc7c
2 changed files with 14 additions and 0 deletions

View File

@ -17,6 +17,7 @@ package template
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"regexp"
"strings" "strings"
"time" "time"
"unicode" "unicode"
@ -117,3 +118,8 @@ func uppercaseFirst(s string) string {
return s return s
} }
func regexReplace(pattern string, input string, replacement string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(input, replacement)
}

View File

@ -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)
}
}