Merge pull request #542 from ralphocdol/custom-api-replace-all-regex

Add replaceMatches
This commit is contained in:
Svilen Markov 2025-04-09 17:07:43 +01:00 committed by GitHub
commit 0bc31b0f31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -389,6 +389,7 @@ The following helper functions provided by Glance are available:
- `trimSuffix(suffix string, str string) string`: Trims the suffix from a string.
- `trimSpace(str string) string`: Trims whitespace from a string on both ends.
- `replaceAll(old string, new string, str string) string`: Replaces all occurrences of a string in a string.
- `replaceMatches(pattern string, replacement string, str string) string`: Replaces all occurrences of a regular expression in a string.
- `findMatch(pattern string, str string) string`: Finds the first match of a regular expression in a string.
- `findSubmatch(pattern string, str string) string`: Finds the first submatch of a regular expression in a string.
- `sortByString(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a string key in either ascending or descending order.

View File

@ -471,6 +471,13 @@ var customAPITemplateFuncs = func() template.FuncMap {
"replaceAll": func(old, new, s string) string {
return strings.ReplaceAll(s, old, new)
},
"replaceMatches": func(pattern, replacement, s string) string {
if s == "" {
return ""
}
return getCachedRegexp(pattern).ReplaceAllString(s, replacement)
},
"findMatch": func(pattern, s string) string {
if s == "" {
return ""