gpt4 book ai didi

javascript - 使用nodejs在字符串末尾添加双引号失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:02 24 4
gpt4 key购买 nike

我碰巧有一个很大的翻译文件,需要在字符串末尾添加双引号。我的目标。

input  string ===> _your_report_is_being_prepared = Your report is being prepared

所需输出 ===> "_your_report_is_being_prepared": "您的报告正在准备中"到目前为止,我碰巧已经成功,但该字符串末尾缺少双引号。

"_your_report_is_being_prepared " : " Your report is being prepared
// how do i add the double quote to the end of the string above.
function stringManipulator(result){
var finalResult;
//1st step --> split string by newline ('\n')
var str = result.split('\n'); // good.
for (var i = 0; i < str.length; i++) { //because iam handling like 600 lines of text...

// convert the resultArray into string so that i can apply string method like replace
var add_quotes = str[i].toString()

//replace the any occurence of (=) with the (:) using the regex pattern of
add_quotes = add_quotes.replace(/=/g, ":" )

// suppose u split ur string further by : so that u can add the double quotes to seperate strings
var resultA = add_quotes.split(':');
//var y = '"' + resultA[0] + '"' + ':' + '"' + resultA[1] + '"';
// output that i got ==> "_access_folders ":" View folders
var y = '"' + resultA[0] + '"' + ' : ' + '"' + resultA[1] + '"'; //===> close
console.log(y) // "_access_folders ":" View folders

finalResult = y ;
}

return finalResult
}

从下面的评论中,它已经测试了代码片段,并且它在浏览器中运行得很好,但在 NodeJS 脚本中运行得不好......但我想用 NodeJS 来实现它。也许让我们编辑问题标题以反射(reflect)nodejs

最佳答案

您可以使用replace=替换为冒号并显式添加双引号:

var str = '_your_report_is_being_prepared = Your report is being prepared'
var res = str.replace(' = ', "\":\"");
res = '"'+ res + '"';
console.log(res);

关于javascript - 使用nodejs在字符串末尾添加双引号失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com