How to Replace Double Quotes with Single Quotes

How to replace all double quotes to single quotes using jquery?

Use double quote to enclose the quote or escape it.

newTemp = mystring.replace(/"/g, "'");

or

newTemp = mystring.replace(/"/g, '\'');

How to replace double quotes nested in another double quotes with single quotes

You could keep track of the depth of the nesting of quotes (whether or not double), and replace them in such a way that on even depths they are double quotes and on odd depths they are single quotes. One precaution is to exclude the apostrophe in ’s (There might be a few other exceptional cases where the apostrophe should not be taken as the close of a quote):

let s = "“Here’s some extra text at the beginning “abc” and at the end”";

let depth = 0;
let result = s.replace(/[“”‘]|’(?!s)/g, m =>
"“‘".includes(m) ? "“‘"[depth++] : "”’"[--depth]);

console.log(result);

How do I replace double quotes with single quotes

str_replace('"', "'", $text);

or Re-assign it

$text = str_replace('"', "'", $text);

Replace all double quotes with single quotes

You need to pass the g flag to sed:

sed "s/\"/'/g"

Python List: Replace Double Quotes with Single Quotes

Solution is to replace if x == "" with if x == '""'

row1 = []
for x in values:
if x == '""':
row1.append('')
else:
row1.append(x)

print(row1)

values = ['50.38146', '', '', 'ABC']

I want to replace single quotes with double quotes in a list

You cannot change how str works for list.

How about using JSON format which use " for strings.

>>> animals = ['dog','cat','fish']
>>> print(str(animals))
['dog', 'cat', 'fish']

>>> import json
>>> print(json.dumps(animals))
["dog", "cat", "fish"]

import json

...

textfile.write(json.dumps(words))

Replace double quotes with single quotes between pairs of square brackets

You can omit the capture group from the pattern as you are replacing on the whole match.

The pattern \[(\".*?\",?)*\] can also match a , at the end, and uses a non greedy match between the double quotes.

What you might do is use a negated character class [^"]* instead to match all between the double quotes, and optionally repeat a non capture group(?:\s*,\s*"[^"]*")* with a leading comma to not allow a comma at the end.

re.sub can use a lambda where you might also do the replacement of the double quotes.

import re

a = """abcda"sd"asd["first", "last", "middle", "dob", "gender"]"""
a = re.sub(r'\["[^"]*"(?:\s*,\s*"[^"]*")*', lambda m: m.group().replace('"', "'"), a)
print(a)

Output

abcda"sd"asd['first', 'last', 'middle', 'dob', 'gender']


Related Topics



Leave a reply



Submit