# Easier way to replace array with array?

**URL:** https://discourse.gohugo.io/t/easier-way-to-replace-array-with-array/5398
**Category:** support
**Created:** [February 9, 2017, 5:59pm UTC](https://discourse.gohugo.io/t/easier-way-to-replace-array-with-array/5398 "2017-02-09T17:59:12Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![JLKM](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@JLKM](https://discourse.gohugo.io/u/JLKM)
#### Post date: [February 9, 2017, 5:59pm UTC](https://discourse.gohugo.io/t/easier-way-to-replace-array-with-array/5398/1 "2017-02-09T17:59:13Z")

</div>

You often need to replace a substring with another substring in a string variable. And almost as often you need to do this operation sevarl times.

From this I would have expected a consecutive string replace to be quite straightforward in a Go template. But it does’t seem to be the case.

At least I didn’t manage to find an easy solution for chaining this `{{ replace .Title "-" " " }}`

And the following howegrown desperado hacks didn’t work:

```auto
{{ replace .Title "aa" "æ" | replace "oe" "ø" | replace "aa" "å" }}

{{ replace .Title ["-", "aa", "oe", "aa"] [" ", "æ", "ø", "å"] }}

{{ .Title | replace "-" " " | replace "aa" "æ" | replace "oe" "ø" | replace "aa" "å" }}

```

After quite some search this post about [maps and dict](https://discuss.gohugo.io/t/how-can-i-make-pairs-of-information-in-the-front-matter/4948) got me on track.

So now this code does the job and turn my non-scandinavian urls into words with special scandinavian letters:

```auto
{{ $pairs := (dict "-" " " "ae" "æ" "oe" "ø" "aa" "å") }}
{{ $.Scratch.Set "specialTitle" .Title }}
{{ range $key, $val := $pairs }}
    {{ $.Scratch.Set "specialTitle" (replace ($.Scratch.Get "specialTitle") $key $val) }}
{{ end }}
{{ $.Scratch.Get "specialTitle" }}

```

But it seems like overkill for a rather simple task. Or have I missed the smart way?

If not: How about making use of the [replacer class](https://www.dotnetperls.com/replace-go) in Go and offer `replacer`-functionality in Go templates?

---

_[View the full topic](https://discourse.gohugo.io/t/easier-way-to-replace-array-with-array/5398)._
