[RUBY] Precautions when replacing backticks with gsub

When posting to Teams via API, the backslash ('`') in the string was processed by Markdown and looked strange, so trying to escape with a backslash ('') in front did not work. ..

example.rb


s = 'abc`123`xyz'  # 'abc\`123\`xyz'I want to
puts s.gsub(/`/, '\`') # => abcabc123abc`123xyz

If you wonder why this is so ...,'\ `'seems to be used as a matched substring in the replacement string.

instance method String#gsub

I increased the backslash and it worked

example.rb


s = 'abc`123`xyz'
puts s.gsub(/`/, '\\\`') # => abc\`123\`xyz

Even so, I want the ability to post with Markdown disabled. .. ..

Recommended Posts

Precautions when replacing backticks with gsub
Precautions when creating PostgreSQL with docker-compose
[Java] Precautions when comparing character strings with character strings
[Rails] Precautions when comparing date and time with DateTime
Precautions when generating a table with a composite key with Iciql + SQLite
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
Error when playing with java
[Ansible] Precautions when testing with docker driver with molecule v3.1 or later