1

As you know, functions such as replace-regexp-in-string and replace-match have an argument LITERAL, meaning ...

If optional ... arg LITERAL is non-nil, insert NEWTEXT literally.
Otherwise treat ‘\’ as special ...

My intent is to replace all occurrences of a single-quote with a back-slash followed by a single-quote. However, the output does not evaluate that way:

 (replace-regexp-in-string "'" "\'" "a'b'c" nil t)

"a'b'c"

 (replace-regexp-in-string "'" "\\'" "a'b'c" nil t)

"a\\'b\\'c"

The result I need, by hook or by crook, is:

"a\'b\'c"

user1404316
  • 769
  • 5
  • 12
  • @npostavs How is that a helpful comment? Anyway, no, I'm not confused, but I do need to generate the text string as I specified. – user1404316 May 16 '18 at 20:16
  • 1
    Backslash will be escaped in displayed strings. Try inserting the return value of this function to see that it does what you want it to. –  May 16 '18 at 20:22
  • 1
    @user1404316 okay, let me rephrase it then. I'm confused by your question as to whether you want a string composed of `a` backslash apostrophe etc, or if you want the string represented by the Lisp string literal `"a\'b\'c"` – npostavs May 16 '18 at 20:40
  • @DoMiNeLa10 You're quite right! If you post your comment as answer, I can accept it and mark the question closed. Thanks. – user1404316 May 16 '18 at 20:44

1 Answers1

2

Elisp displays strings in a readable format, where characters have to be escaped, and in this case it displays \ as \\, because it has to be escaped.

Try inserting the return value like this to see that it's doing what you want it to:

(insert (replace-regexp-in-string "'" "\\'" "a'b'c" nil t))