Merge pull request #4 from steadyapp/master

Adding custom helper for regex
This commit is contained in:
Thomas Boerger 2018-10-04 07:18:23 +02:00 committed by GitHub
commit 4019baa6c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 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"
@ -39,6 +40,7 @@ var (
"lowercase": strings.ToLower, "lowercase": strings.ToLower,
"trim": strings.TrimSpace, "trim": strings.TrimSpace,
"title": strings.Title, "title": strings.Title,
"regexReplace": regexReplace,
} }
) )
@ -117,3 +119,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)
}
}