# 在正则表达式中转义反斜杠

**URL:** <https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641>\
**Category:** Support\
**Created:** [2024年九月3日 11:21 UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641 "2024-09-03T11:21:13Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)\
**Post date:** [2024年九月3日 19:20 UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/2 "2024-09-03T19:20:47Z")

</div>

我不确定这是否能奏效。

既然您只想将“T8”替换为“T9”。也许可以删除所有斜杠并进行测试？

在您的语法错误中，它显示您缺少引号

```plaintext
SyntaxError: unexpected integer literal, expecting ')'
  topic.title.gsub!(/T8/2024/,"T9/2024")
                        ^~~~

```

也许试试这个修复了缺失的引号

```plaintext
Topic.where("title LIKE '%T8/2024%'").find_each do |topic|
  topic.title.gsub!("T8/2024","T9/2024")
  topic.fancy_title.gsub!(/T8/2024","T9/2024")
  topic.slug.gsub!("T8/2024","T9/2024")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

```

还删除了多余的“/”

---

_[View the full topic](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641)._
