Searching for a way to automatically replace strings

I use the following template to create a page with all my weekly snippets.

```template

{{#each { page where name =~ /^Journal\/Week\// order by name desc } }}
**-----------------------------**
## {{replace(name, "Journal/Week/", "Week of: ")}}
**-----------------------------**

{{readPage(name)}}
---
---

{{/each}}
```

This basically take all my pages in /Journal/Week/* and adds them into the one where the template is written.

Does anyone know how I can add a replace( command to replace strings within each one of those pages automatically?

Ex: Let’s say I’d like to replace all http:// with https://

Thanks

Does this work for you? Paste it somewhere in the space and run {[System: Reload]} command

```space-script
silverbullet.registerFunction("replace", (text, searchValue, replaceValue) => {
  return text.toString().replaceAll(searchValue, replaceValue);
});
```

I tried it with the template in your post, looks really cool :+1:

Thanks for trying to help me with this :slight_smile:
I didn’t manage to make it work.

This is what I tried.

I added the space-script exactly as you wrote it in my Space-Script page
I added the following “test” in my template

```template

{{#each { page where name =~ /^Journal\/Week\// order by name desc } }}
**-----------------------------**
## {{replace(name, "Journal/Week/", "Week of: ")}}
**-----------------------------**

{{readPage(name)}}
{{replace("Management","Mgmt"}}
---
---

{{/each}}
```

I perfomed a System: Reload

I have many subsections in my snippets called Management but no text was replaced.

Any idea how to solve this?

Thanks a lot

There are two issues here:

The minor one is that you’re missing closing parenthesis for replace function call (before closing double brace)

The main thing is that replace only can change things you pass as first argument, just like you did above for name. So I think what you need is

{{replace(readPage(name), "Management", "Mgmt")}}

Disclaimer: I’m not at my computer right now, all of this response is untested

Thanks again for your fast response :slight_smile:

The missing closing parenthesis was just a typo when writing this post. But you caught it, great eye :slight_smile:

{{replace(readPage(name), "Management", "Mgmt")}}

works… but i realize that I need to remove

{{readPage(name)}}

with it included, all the pages were embedded twice, once raw and the second time with the changes.

So, basically my template looks now like this:

```template

{{#each { page where name =~ /^Journal\/Week\// order by name desc } }}
**-----------------------------**
## {{replace(name, "Journal/Week/", "Week of: ")}}
**-----------------------------**

{{replace(readPage(name),"Management","Mgmt")}}


---
---

{{/each}}
```

This now makes me wonder how I can make multiple replaces at the same time, as there are several things that i want to fix in this import of my weekly snippets so the links work inside SB. :thinking:

Another thing,
Your space-script doesn’t allow Regex replacemetnt.

But your script has made me realize that i can use the default replace function to perform the search and replace.

I still have the problem on how to execute multiple replace within the same template

I found a way to do multiple replaces by nesting them, but it becomes unfeasible when trying to do more than 2 or 3.

```template

{{#each { page where name =~ /^Journal\/Week\// order by name desc } }}
**-----------------------------**
## {{replace(name, "Journal/Week/", "Week of: ")}}
**-----------------------------**

{{replace( 
    replace(readPage(name), /(url\/)(?=\d+)/, "http://url/"), 
        "Mgmt", "Management" 
)}}

---
---

{{/each}}
```

The replace function is also a built in, it shouldn’t be hard to extend it to support a whole bunch of string replacement pairs instead of just 1.

1 Like

That would be amazing, if you could extend it.

I ended up creating a script file that i run in the terminal and made all the changes.
But this is not optimal for me as I wouldn’t want to every week manually change files so the links work in SB.
If i can use multiple replacement pairs within SB and with Regex, it would be incredible.