【发布时间】:2018-10-08 03:23:56
【问题描述】:
我碰巧有很大的翻译文件,需要在字符串末尾添加双引号。 我的目标。
input string ===> _your_report_is_being_prepared = Your report is being prepared
desired output ===> "_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
}
从下面的 cmets 中,它已经测试了代码 sn-p,它在浏览器中运行良好,但在 nodejs 脚本中却没有......但我想用 nodejs 来实现它。也许让编辑问题标题以反映 nodejs
【问题讨论】:
-
您的代码运行良好。请分享一个可以证明您的问题的有效 sn-p。
标签: javascript node.js