Replace all quotes in a string with escaped quotes?
NickName:mix Ask DateTime:2013-09-19T13:09:56

Replace all quotes in a string with escaped quotes?

Given a string in python, such as:

s = 'This sentence has some "quotes" in it\n'

I want to create a new copy of that string with any quotes escaped (for further use in Javascript). So, for example, what I want is to produce this:

'This sentence has some \"quotes\" in it\n'

I tried using replace(), such as:

s.replace('"', '\"')

but that returns the same string. So then I tried this:

s.replace('"', '\\"')

but that returns double-escaped quotes, such as:

'This sentence has some \\"quotes\\" in it.\n'

How to replace " with \"?

UPDATE:

I need as output from this copyable text that shows both the quotes and the newlines as escaped. In other words, I want to be able to copy:

'This sentence has some \"quotes\" in it.\n'

If I use the raw string and print the result I get the correctly escaped quote, but the escaped newline doesn't print. If I don't use print then I get my newlines but double-escaped quotes. How can I create a string I can copy that shows both newline and quote escaped?

Copyright Notice:Content Author:「mix」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18886596/replace-all-quotes-in-a-string-with-escaped-quotes

Answers
Yussuf S 2013-09-19T05:33:23

Hi usually when working with Javascript I use the json module provided by Python. It will escape the string as well as a bunch of other things as user2357112 has pointed out.\n\nimport json\nstring = 'This sentence has some \"quotes\" in it\\n'\njson.dumps(string) #gives you '\"This sentence has some \\\\\"quotes\\\\\" in it\\\\n\"'\n",


Tim Peters 2013-09-19T05:20:36

Your second attempt is correct, but you're getting confused by the difference between the repr and the str of a string. A more idiomatic way of doing your second way is to use \"raw strings\":\n\n>>> s = 'This sentence has some \"quotes\" in it\\n'\n>>> print s\nThis sentence has some \"quotes\" in it\n\n>>> print s.replace('\"', r'\\\"') # raw string used here\nThis sentence has some \\\"quotes\\\" in it\n\n>>> s.replace('\"', r'\\\"')\n'This sentence has some \\\\\"quotes\\\\\" in it\\n'\n\n\nRaw strings are WYSIWYG: backslashes in a raw string are just another character. It is - as you've discovered - easy to get confused otherwise ;-)\n\nPrinting the string (the 2nd-last output above) shows that it contains the characters you want now.\n\nWithout print (the last output above), Python implicitly applies repr() to the value before displaying it. The result is a string that would produce the original if Python were to evaluate it. That's why the backlashes are doubled in the last line. They're not in the string, but are needed so that if Python were to evaluate it each \\\\ would become one \\ in the result.",


More about “Replace all quotes in a string with escaped quotes?” related questions

Replace all quotes in a string with escaped quotes?

Given a string in python, such as: s = 'This sentence has some "quotes" in it\n' I want to create a new copy of that string with any quotes escaped (for further use in Javascript). So, for example,

Show Detail

Match and replace a word not in quotes (string contains escaped quotes)

How can I replace all occurrences of a particular word from a given string with another, provided the word does not occur within quotes. Note that the input string will most likely contain escaped ...

Show Detail

Replace single quotes in a string but not escaped single quotes

I'm making a Python API get request and getting JSON results which I have captured in a string. The string is similar to a string literal containing a dictionary: {'key1': 4, 'key2': 'I\'m home'}. ...

Show Detail

Regex to replace non escaped Quotes

After having some trouble building a json string I discovered some text in my database containing double quotes. I need to replace the quotes with their escaped equivalents. This works: function e...

Show Detail

How do I find and replace all escaped quotes and regular quotes with different values?

I'm looking to replace all occurrences of an escaped quote (\") with (\\\") in the string, then replacing all remaining unescaped quotes (") with escaped quotes (\"). Here's what I tried so far: ...

Show Detail

Using Regex/Replace to replace escaped quotes with quotes, unless they would be in a string after the replace

I am receiving JSON from an API. Unfortunately, all the nested objects are returned as strings. I am trying to use .replace() to convert the string to a JSON object. So "{ -> {, }" -&g...

Show Detail

How to replace single quotes with escaped single quotes in ruby

I'm trying to replace single quotes (') with escaped single quotes (\') in a string in ruby 1.9.3 and 1.8.7. The exact problem string is "Are you sure you want to delete '%@'". This string should

Show Detail

How to replace single quotes string with literal if it contains escaped single quotes?

I need to escape all single quotes strings with literals and I am using the following regular expression: '[^']*' It is working fine, except when I have escaped single quotes in the string that m...

Show Detail

Remove white spaces unless within quotes, ignoring escaped quotes

I have a JSON string in which I would like to remove all white spaces that are not within quotes. I searched online and I already found a solution, which is the following: aidstring = Regex.Replace(

Show Detail

Replace escaped double quotes in CSV

I am reading content from a csv. I want to replace the single and double quotes from the data. I have tried the following code but is not working. $resource = fopen($this->csv_file, 'r');

Show Detail