RegEx patterns should be in backticks, not quotes

Learning the hard way trying to run this regex:

{{ replaceRE "^\d*(-| |)" "" .Params.name }}    (expected 20-test-3  -> test-3)

Would fail but it wouldn’t say why. Whittling it down it looked like the \d was the problem, which is frustrating because it is typical syntax for a digit. I had the idea that maybe the \ needed to be escaped for some reason, and when I did \\d it worked as expected… but looking on the forum, it looks like the better solution from @jmooring (Thank you!) is to surround the pattern with backticks instead to make it a string literal and not have to escape the backslashes.

{{ replaceRE `^\d*(-| |)` "" .Params.name }}

Unfortunately, the docs for findRE and replaceRE both use double-quotes, don’t explain about the backticks, and don’t have extensive examples, but the findRE does have an example of using \n in double quotes without escaping so I don’t know if that will fail as mine did because it’s not escaped or if it’ll work for some special reason.

5 Likes