Refactor multiple replace statements


#1

With the following code, is there a way to do this more neatly instead of using multiple replace statements?

{{$a := delimit .Params.products ", "}}
{{$a := replace $a "A, " ""}}
{{$a := replace $a "B, " ""}}
{{$a := replace $a ", C" ""}}
{{$a := replace $a "D, " ""}}
{{$a := split $a ","}}

I’ve tried doing the following which works but does not do what I need since it omits the ,.

{{$a := delimit .Params.products ", "}}
{{$b := "A, B, C, D"}}

{{$a := replace $a $b ""}}
{{$a := split $a ","}}

If I do something like this I get the error: can't give argument to non-function "A"

{{$a := delimit .Params.products ", "}}
{{$b := "A, " "B, " ", C" "D, "}}

{{$a := replace $a $b ""}}
{{$a := split $a ","}}

#2

You can pipe them or use replaceRE (if there is a pattern to match), or create a dict first and then replace. See the functions at the Docs.


Exclude term from being displayed in array