Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace .toString() single quotes with double quotes

Perhaps a strange request, but I'm using CouchDB views, which requires a string to be surrounded by double quotes.

This works:

?key=["test","234"]

This won't work:

?key=['test','234']

So in my NodeJS application, I'm trying to build up the correct key to pass to CouchDB

var key1 = "test";
var key2 = "234";

{ key: [key1, key2] }

This always comes out as

{ key: [ 'test', '234' ] }

Is there any effective way to get my desired output? (double quotes)

like image 838
woutr_be Avatar asked Jan 11 '16 09:01

woutr_be


People also ask

How do you replace single quotes with double quotes?

Use the String. replace() method to replace double with single quotes, e.g. const replaced = str. replace(/"/g, "'"); . The replace method will return a new string where all occurrences of double quotes are replaced with single quotes.

How do you replace a single quote in a string?

Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.

How do you insert a double quote into a quoted string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you display double quotes in a string?

To place quotation marks in a string in your code In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark. For example, to create the preceding string, use the following code. Insert the ASCII or Unicode character for a quotation mark. In Visual Basic, use the ASCII character (34).


1 Answers

CouchDB does not need double-quotes per se; it needs JSON-encoded parameters.

That's good news for you! Just build your key however you like in JavaScript:

var key = [
    'test',
    '234'
]

And then JSON-encode it before sending to CouchDB:

key = JSON.stringify(key) // Result: the string '["test","234"]'
like image 114
JasonSmith Avatar answered Oct 02 '22 13:10

JasonSmith