1

i have a function of downloading excel file. two of the headers shipping addressline1 and 2 can contain character ",". which i want to be replaced by " ". i tried using replace(/,/g,' ') but doesnt work.

for(i=0;i<columns.length;i++){
                if(indexesNotToInclude.indexOf(i)>=0){continue;}
                if(firstColumn){
                    firstColumn=false;
                }
                else{
                    csvString += ",";
                }

                console.log(columns[i].name+":"+item[columns[i].name]);
                csvString += item[columns[i].name];
                if([columns[i].name]=='ShippingAddressLine1'||[columns[i].name]=='ShippingAddressLine2')
                {

                    csvString += (item[columns[i].name]).replace(/,/g,' ');
                }
            }
            csvString += "\r\n";
4
  • your try looks correct. if you console.log(item[columns[i].name]) before doing the replace, do you see the coma ?
    – ben
    Jul 6, 2015 at 13:17
  • then the replace should work (as this jsfildle demonstrate: jsfiddle.net/grucgahh) Can you provide the content of columns and item so we can try with your real data ?
    – ben
    Jul 6, 2015 at 13:31
  • SHIPPINGADDRESSLINE1 523 Nana thakor vada junior masjid paase., anand, Gujarat, 388205 SHIPPINGADDRESSLINE2 Anand GJ IN 388205 9723948144 Delhivery 383510003430 Prepaid PayU Delivered Jul 7, 2015 at 5:36
  • hmmm, you'd better edit your question and use a script snippets... This is not helpful
    – ben
    Jul 7, 2015 at 7:26

1 Answer 1

0

Here is a demo of replacing comma character with an empty string:

var mystring = "this,is,a,test"  
console.log("Before: " + mystring);
mystring = mystring.split(',').join(' ');
console.log("After: " + mystring);

Demo

3
  • For someone unexperienced with regex, regex approach is totally unclear, while the other approach is clear even you do not know javascript (you understand what is going on).
    – bjaksic
    Jul 6, 2015 at 13:38
  • then, as other have pointed out in the comments to the question, the problem is not in splitting, but somwhere else. As you will not provide the data (columns, item, ...) or an example with dummy data, there is not much we can do.
    – bjaksic
    Jul 6, 2015 at 13:52
  • SHIPPINGADDRESSLINE1 523 Nana thakor vada junior masjid paase., anand, Gujarat, 388205 SHIPPINGADDRESSLINE2 Anand GJ IN 388205 9723948144 Delhivery 383510003430 Prepaid PayU Delivered Jul 6, 2015 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.