Skip to content
Advertisement

removing or replacing comma from string imported from CSV Data where object is between bracket or any other Regex Javascript [closed]

I have searched in many places could not find a solution. I am importing Data from CSV file and then want to create array from this there for I want to replace comma delimiter with any other delimiter so when I split string object with comma should be as it is

I have a string:

VAR newString=Date,Narration,Debit_Account1,  Debit_Amount1  ,Debit_Account2,  Debit_Amount2  ,Debit_Account3, Debit_Amount3 ,Debit_Account4,  Debit_Amount4  ,Credit_Account,  Credit_Amount  
01-04-21,payment to party1,PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),HDFC BANK,( 50,000.00 )
01-04-21,payment to party1,PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),HDFC BANK,( 50,000.00 )

I want to remove only those commas which are not between brackets ().

I have tried to do this with replace method but could not understand how to give condition for escaping value inside brackets.

Advertisement

Answer

I found My answer thank you for sawing interest in my question answer is

var newstring =Date,Narration,Debit_Account1,  Debit_Amount1  ,Debit_Account2,  Debit_Amount2  ,Debit_Account3, Debit_Amount3 ,Debit_Account4,  Debit_Amount4  ,Credit_Account,  Credit_Amount  
01-04-21,payment to party1,PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),HDFC BANK,( 50,000.00 )
01-04-21,payment to party1,PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),PARTY1,( 50,000.00 ),HDFC BANK,( 50,000.00 )

var answerstring=newstring.replace(/([^)]*)/g,(match,startIndex,wholeString)=>{
return match.replace(/,/g,"<>")
})
var newString2=answerstring.replace(/,/g,"_").replace(/<>/g,",");

/*this will give answer as below 
Date_Narration_Debit_Account1_  Debit_Amount1  _Debit_Account2_  Debit_Amount2  _Debit_Account3_ Debit_Amount3 _Debit_Account4_  Debit_Amount4  _Credit_Account_  Credit_Amount  
01-04-21_payment to party1_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_HDFC BANK_( 50,000.00 )
01-04-21_payment to party1_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_PARTY1_( 50,000.00 )_HDFC BANK_( 50,000.00 )

*/

I did not understand formula correctly but it did the job if any one can elaborate this formula please post so other can understand formula

Advertisement