# 在所有帖子中替换字符串

**URL:** https://meta.discourse.org/t/replace-a-string-in-all-posts/48729
**Category:** Self-Hosting
**Tags:** how-to
**Created:** [2016年八月17日 10:23 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729 "2016-08-17T10:23:49Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年一月17日 19:18 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/166 "2023-01-17T19:18:55Z")

</div>

```plaintext
Topics.where(category_id: 123).each do |t|
  posts.where(topic_id: t).each do |p|
    p.raw.gsub!("replaceme","/")
    p.save
  end
end

```

> [@MiG](#):
>
> 只需输入“rails”，然后复制其中的代码？

```plaintext
./launcher enter app
rails c
# 粘贴内容

```

你或许应该先备份一下。

---

<div class="post-metadata">

### Author: ![MiG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mig/32/119486_2.png) [@MiG](https://meta.discourse.org/u/MiG)
#### Post date: [2023年一月17日 19:42 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/167 "2023-01-17T19:42:03Z")

</div>

谢谢 Jay，感谢你的时间！

我也吃过亏，知道在做任何重要的事情之前都要先备份 🙂

更新：它运行得非常好，但我不得不使用 `\\n` 而不是 `/`。

另外，上面的代码是 `Topic` 和 `Post` 🙂

运行此代码后进行一次重烘焙也是个好主意。在我的例子中，是为了重新生成缩略图。

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [2024年二月9日 15:43 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/169 "2024-02-09T15:43:01Z")

</div>

帖子已合并到现有主题：[使用正则表达式替换多个主题中的内容？](https://meta.discourse.org/t/replacing-content-in-multiple-topics-with-regex/294828/7)

---

<div class="post-metadata">

### Author: ![Fred\_Koehler](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fred_koehler/32/293835_2.png) [@Fred\_Koehler](https://meta.discourse.org/u/Fred_Koehler)
#### Post date: [2024年三月9日 13:29 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/170 "2024-03-09T13:29:22Z")

</div>

嘿 @nathank！想知道你是否找到了这个问题的上游解决方案。我们正在处理一个类似的问题，我们希望在用户每次在主题或帖子中包含“X”时，（基本上）自动将“X”替换为“Y”。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年三月9日 14:21 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/171 "2024-03-09T14:21:18Z")

</div>

这就是“监视词”功能的作用。它会在创建或编辑帖子时替换字符串。

/admin/customize/watched\_words

---

<div class="post-metadata">

### Author: ![Fred\_Koehler](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fred_koehler/32/293835_2.png) [@Fred\_Koehler](https://meta.discourse.org/u/Fred_Koehler)
#### Post date: [2024年三月9日 15:15 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/172 "2024-03-09T15:15:12Z")

</div>

谢谢 Jay！看起来这对于基于字符的序列来说是可行的，但对于非字符格式（如制表符缩进）则不行。

用例：我们平台上有几位老作家仍然像对待打字机一样对待键盘，按 5 次空格键进行缩进。其他作家则复制粘贴带有制表符缩进的内容。这两种情况都会导致 Discourse 将其解释为标记。（如果我用词正确的话。）

在主题组件方面找到了一个变通方法，我们使用 CSS 来覆盖标记格式，而这对于我们的特定用例来说是不需要的。

感谢您的帮助！

---

<div class="post-metadata">

### Author: ![sniper756](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sniper756/32/545002_2.png) [@sniper756](https://meta.discourse.org/u/sniper756)
#### Post date: [2025年十二月9日 10:46 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/173 "2025-12-09T10:46:49Z")

</div>

> 如果我想删除[JUSTIFY]，这样对吗？
> 
> rake ‘posts:delete\_word[[JUSTIFY]]’

---

<div class="post-metadata">

### Author: ![sniper756](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sniper756/32/545002_2.png) [@sniper756](https://meta.discourse.org/u/sniper756)
#### Post date: [2025年十二月9日 11:55 UTC](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/174 "2025-12-09T11:55:29Z")

</div>

我使用了以下命令对xenforo迁移过来没有转换的[HEADING]进行替换

```plaintext
sudo -u discourse -H RAILS_ENV=production bundle exec rails runner '
Post.where("raw LIKE ?", "%[HEADING%").find_each(batch_size: 500) do |p|
  orig = p.raw
  cooked = orig.dup
  (1..6).each do |lvl|
    cooked.gsub!(/\[HEADING=#{lvl}\](.*?)\[\/HEADING\]/mi) { "#" * lvl + " " + $1 }
  end
  cooked.gsub!(/\[HEADING\](.*?)\[\/HEADING\]/mi) { "## " + $1 }
  next if cooked == orig
  p.raw = cooked
  p.save!(validate: false) # 触发 rebake，但跳过验证
end
puts "HEADING tags converted & rebaked."
'

```

[上一頁](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729.md?page=1)
