I am attempting to print a string as a string literal in JavaScript, so that the string will be printed exactly as it is written:
printStringLiteral("\n\nHi!");//this prints "Hi!" instead of "\n\nHi!".
//What will I need to do in order to print
//the string as a string literal instead?
function printStringLiteral(toPrint){
console.log("\"" toPrint + "\"");
}
您可以使用JSON:
JSON.stringify(toPrint);
printStringLiteral("\\\n\\\nHi!");
function printStringLiteral(toPrint){
console.log("\\\"" + toPrint + "\\\"");
}
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.