# בריחת סקייפ אחורה ב-regex

**URL:** https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641
**Category:** Support
**Created:** [3 בספטמבר,‏ 2024,‏ 11:21am UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641 "2024-09-03T11:21:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![thaidb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thaidb/32/68488_2.png) [@thaidb](https://meta.discourse.org/u/thaidb)
#### Post date: [3 בספטמבר,‏ 2024,‏ 11:21am UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/1 "2024-09-03T11:21:13Z")

</div>

I wanna replace string title from “T8/2024” to “T9/2024”

from topic [How to replace a string/character in topic title? - #11 by pfaffman](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/11)

```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

```

**But i get error:**

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

```

If the word you want to replace has the character “/”, how should we handle it?

---

<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: [3 בספטמבר,‏ 2024,‏ 7:20pm UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/2 "2024-09-03T19:20:47Z")

</div>

I don’t know off hand if this would work or not

Since you want to only replace “T8” with “T9”. Maybe remove all slashes and test?

In your syntax error it shows your missing quotes

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

```

Maybe try this fixed missing quotes

```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

```

Also removed extra “/”

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [3 בספטמבר,‏ 2024,‏ 7:41pm UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/4 "2024-09-03T19:41:15Z")

</div>

> [@thaidb](#):
>
> If the word you want to replace has the character “/”, how should we handle it?

Escape it with `\`:

```plaintext
[1] pry(main)> 'A title covering T8/2024'.gsub(/T8\/2024/, 'T9/2024')
=> "A title covering T9/2024"

```

---

<div class="post-metadata">

### Author: ![thaidb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thaidb/32/68488_2.png) [@thaidb](https://meta.discourse.org/u/thaidb)
#### Post date: [6 בספטמבר,‏ 2024,‏ 12:54am UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/5 "2024-09-06T00:54:36Z")

</div>

Everything is smooth. Thank you so much, 🙂

```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

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [6 באוקטובר,‏ 2024,‏ 12:55am UTC](https://meta.discourse.org/t/escaping-a-backslash-in-a-regex/324641/6 "2024-10-06T00:55:26Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
