replaceRE returns generic syntax error

I’m a regex noob, so this may be a silly question, but why does the following not work?

{{ $highlighted := replaceRE "\?\?(.*?)\?\?" "<mark>$1</mark>" "??thing to highlight??" }}

The regex appears to be valid when testing it in http://regexr.com/, but I just get a generic syntax error in my console. If I break the regex, a regex-specific error comes out — for example, if I unescape the “?” marks. I tried it without the markup around the capture group in the second argument too, but no luck.

Thanks!

Try using https://regex101.com/ with “golang” selected.

Thanks but I don’t see the difference. \?\?(.*?)\?\? works with golang selected. Is there anything I have to wrap the expression in?

Hi @Heydon - I checked this out and it’s not working for me either. I’m not sure, but I think maybe your right about a missing flag or something (just looking around the forums), but TBH I don’t know. My first post was just to make sure you were checking your syntax with the same type of parser that Hugo uses.

1 Like

Thanks for getting back to me @budparr.

It doesn’t seem to be a regression because the example from the docs appears to work, so I’m not sure what’s going on.

You need to escape your backslashes in the pattern string.

1 Like

I need to escape the backslashes that are used to escape the “?” characters?

Yes, that’s what I mean.

"\\?\\?(.*?)\\?\\?"

You can also use backticks to send a literal string, which doesn’t support escape sequences.

`\?\?(.*?)\?\?`
3 Likes