1

I happen to have large translation file and need to add double quotes to end of the string. My Goal.

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

desired output ===> "_your_report_is_being_prepared" : "Your report is being prepared" I happen to have succeeded until this point but it lacks double quote at end of that string.

"_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
}

From the comments below, it have tested the code snippet and it works perefectly well in the browser but not the in nodejs script... yet i wanted to achieve it with nodejs. maybe let edit the question title to reflect nodejs

3 Answers3

0

You can use replace to replace the = with colon and add double quotes explicitly:

var str = '_your_report_is_being_prepared = Your report is being prepared'
var res = str.replace(' = ', "\":\"");
res = '"'+ res + '"';
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • Thank you ... it is working well in the browser but in the nodejs environment , it is also not adding the double quote at the end. is it a problem of nodejs, i am not sure $ node helloFile.js `"_access_folders":"View folders` `"_access_forms":"View forms` "_access_key":"Access key "_action":"Action – bogere goldsoft Apr 27 '18 at 10:31
0

Your problem was at var resultA = add_quotes.split(':'); line. change this to var resultA = add_quotes.split(' : '); and you are done. Check below working code snippet:

// 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));
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • It seems the above code snippet in working in the browser but not in the nodejs script. it is failing to add the double quote via nodejs environment – bogere goldsoft Apr 27 '18 at 11:19
0

After unsucessful attempts to add the double quotes using nodejs script, i finally settled on using the search and replace tool in sublime text editor Using Search and Replace Ctrl+H with Regex let's find this $ and replace it with " For more information, check this stackoverflow question. How to paste text to end of every line? Sublime 2

Developer Guy
  • 2,318
  • 6
  • 19
  • 37