I am loading some text from a file in a node js application using something similar to the following:
var retr = fs.readFileSync( __dirname + tmp).toString();
The files contains a string similar to: a"a
I am using that string to generate a constant value using double quotes:
var MY_CT ="a"a";
Of course, node complains this code is not valid.
How can I transform the double-quotes to init MY_CT
properly? I can't modify the source file itself. The source file is not HTML, it is plain text.
What I understood is that you have a variable (let's call it x
) which contains a random string, read from a file, which can contain single quotes or double quotes and you want to generate a file containing that string between quotes so it can be interpreted in JavaScript.
The solution would be:
JSON.stringify(x)
This will return in your case: "a\\"a"
which is valid. This will also work when your file contains single quotes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.