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)
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.
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.
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.
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).
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"]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With