我从服务器收到字符串格式的响应,我想使用该语言的正确格式来呈现它,而不是单行代码。
服务器的响应
let response = "function Hero(){ console.log("Hello, world!"); }" [包含开头和结尾的粗体]
我希望它以这样的格式呈现:
function hero(){
console.log("Hello, world!");
}
如有任何意见或建议,我们将不胜感激
你可以通过这样做简单地解决这个问题 -
let response = "function Hero(){ console.log("你好,世界!"); }";
// Remove leading and trailing quotes
response = response.slice(1, -1);
// Indent each line by 2 spaces
let formattedFunction = response.split("\n").map(line => " " + line).join("\n");
console.log(formattedFunction);