无法使用nodejs将双引号添加到字符串的末尾

时间:2018-04-27 09:49:00

标签: javascript node.js

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

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
}

从下面的评论中,它已经测试了代码片段,它在浏览器中运行良好,但在nodejs脚本中却没有...但我想用nodejs实现它。也许让我们编辑问题标题以反映nodejs

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您的问题发生在var resultA = add_quotes.split(':');行。将此更改为var resultA = add_quotes.split(' : ');,您就完成了。请查看以下工作代码段:

// 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
}

var str = "_your_report_is_being_prepared = Your report is being prepared";
console.log(stringManipulator(str));

答案 2 :(得分:0)

在使用nodejs脚本添加双引号的尝试不成功之后,我终于决定在sublime文本编辑器中使用搜索和替换工具 使用搜索和替换 Ctrl + H 与正则表达式让我们找到这个$并将其替换为&#34; 有关更多信息,请查看此stackoverflow问题。 How to paste text to end of every line? Sublime 2