Skip to content
Snippets Groups Projects
Unverified Commit 37818c4e authored by Matt Farina's avatar Matt Farina Committed by GitHub
Browse files

Merge pull request #257 from rheaton/master


Signed-off-by: default avatarMatt Farina <matt@mattfarina.com>
parents ef25c39f 1c12b853
No related branches found
No related tags found
No related merge requests found
......@@ -446,6 +446,17 @@ The above produces `[pi a]`
`regexSplit` panics if there is a problem and `mustRegexSplit` returns an error to the
template engine if there is a problem.
## regexQuoteMeta
Returns a string that escapes all regular expression metacharacters inside the argument text;
the returned string is a regular expression matching the literal text.
```
regexQuoteMeta "1.2.3"
```
The above produces `1\.2\.3`
## See Also...
The [Conversion Functions](conversion.html) contain functions for converting
......
......@@ -340,6 +340,7 @@ var genericMap = map[string]interface{}{
"mustRegexReplaceAllLiteral": mustRegexReplaceAllLiteral,
"regexSplit": regexSplit,
"mustRegexSplit": mustRegexSplit,
"regexQuoteMeta": regexQuoteMeta,
// URLs:
"urlParse": urlParse,
......
......@@ -83,6 +83,11 @@ func TestShuffle(t *testing.T) {
assert.NoError(t, runt(`{{ shuffle "Hello World" }}`, "rldo HWlloe"))
}
func TestRegex(t *testing.T) {
assert.NoError(t, runt(`{{ regexQuoteMeta "1.2.3" }}`, "1\\.2\\.3"))
assert.NoError(t, runt(`{{ regexQuoteMeta "pretzel" }}`, "pretzel"))
}
// runt runs a template and checks that the output exactly matches the expected string.
func runt(tpl, expect string) error {
return runtv(tpl, expect, map[string]string{})
......
......@@ -77,3 +77,7 @@ func mustRegexSplit(regex string, s string, n int) ([]string, error) {
}
return r.Split(s, n), nil
}
func regexQuoteMeta(s string) string {
return regexp.QuoteMeta(s)
}
......@@ -196,3 +196,8 @@ func TestMustRegexSplit(t *testing.T) {
assert.Equal(t, c.expected, len(res), "case %#v", c.args)
}
}
func TestRegexQuoteMeta(t *testing.T) {
assert.Equal(t, "1\\.2\\.3", regexQuoteMeta("1.2.3"))
assert.Equal(t, "pretzel", regexQuoteMeta("pretzel"))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment