I was writing a function to remove the href tag from a return value. Once I figure out the regex I used the documented back tick character to enclose the expression. When I tried to run the test code it would always fail. When I run the same code in the online playground it works just fine. I’ve updated to 1.8.3, but the issue persists. The fix for my code was to enclose the expression in double quotes instead. Anyone know why this inconsistency exists.
Here is a playground example of what I was trying to get to work locally. Why does it work here and not locally?
Here is the function I have working on my side right now. It just doesn’t work with the back ticks for some reason.
The difference is in the MustCompile call. In the function below I have double quotes, but the recommended method is to use back ticks for the string literal. Back ticks work in the playground, but not locally for me. I’m just curious if anyone knows why there is a discrepancy.
func removeHref(text string) string {
var re = regexp.MustCompile("\\n<a[^>]*>(.*?)</a>")
var str = text
var substitution = `$1`
return re.ReplaceAllString(str, substitution)
}
Within a double quote your \\n becomes a \n which the regexp compiler interprets as a newline. In backquotes it remains as \\n which is an escaped backslash followed by a literal n.