【问题标题】:failed to add double quotes to the end of a string using nodejs无法使用nodejs在字符串末尾添加双引号
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 谢谢...它在浏览器中运行良好,但在 nodejs 环境中,它也没有在末尾添加双引号。是不是nodejs的问题,我不确定 $ node helloFile.js "_access_folders":"View folders "_access_forms":"View forms "_access_key":"Access key "_action":"Action
【解决方案2】:

您的问题出在var resultA = add_quotes.split(':'); 行。将其更改为var resultA = add_quotes.split(' : ');,您就完成了。检查下面的工作代码sn-p:

// 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));

【讨论】:

  • 上面的代码 sn-p 似乎在浏览器中工作,但在 nodejs 脚本中没有。它无法通过 nodejs 环境添加双引号
【解决方案3】:

在尝试使用 nodejs 脚本添加双引号失败后,我最终决定使用 sublime 文本编辑器中的搜索和替换工具 使用 Search and Replace Ctrl+H 和 Regex 让我们找到这个 $ 并将其替换为 " 有关更多信息,请查看此 stackoverflow 问题。 How to paste text to end of every line? Sublime 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2023-02-23
    • 1970-01-01
    相关资源
    最近更新 更多