简体   繁体   中英

How to escape all markdown characters with a backslash?

I want to escape all markdown characters with a backslash \\ .

I've tried

"testing _the thing".gsub /(\*|_|`)/, '\\\1'

And the result is:

testing \\_the thing

With only 2 \\\\

"testing _the thing".gsub /(\*|_|`)/, '\\1'
 => "testing _the thing"

The output I would like is:

 => "testing \_the thing"

I've tried many things without luck.

You're getting the right output. In inspect mode you'll see this:

"\\_"

That means " literal -backslash underscore". Remember that inside double-quoted strings the backslash is used for special characters and sequences like \\t for tab or \\n for newline. A backslash must be escaped if you want an actual backslash.

Try this:

puts "\\_"

You'll see just \\_ like you want.

If you do this:

"\_"

That's " literal -underscore", which of course is just an underscore. The backslash ends up disappearing since it's redundant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM