Reputation:
I have a string which looks like this
var dragdropMatchResponseData = '2838[,]02841[:]2839[,]02838[:]2840[,]02840[:]2841[,]02839';
I want to replace the following
1: '[,]' into ':'
2: '[.]' into ','
I tried the following
console.log(dragdropMatchResponseData.replace({ '[,]': ':', '[:]': ',' }));
and
console.log(dragdropMatchResponseData.replace('[,]', ':').replace( '[:]', ','));
but nothing helped me
I want my end result to look like
'2838:02841,2839:02838,2840:02840,2841:02839';
I don't want to add replace in multiple times, I want to do this at one time,
how can I achieve this?
Upvotes: 1
Views: 1694
Reputation: 336
Hey It can be easily achieved using replace
function of JS
var data = '2838[,]02841[:]2839[,]02838[:]2840[,]02840[:]2841[,]02839';
console.log(data.replace(/\[:]/g, ',').replace(/\[,]/g, ':'))
Upvotes: 0
Reputation: 241
Try regular expression
dragdropMatchResponseData.replace(/\[,\]/g, ':').replace(/\[:\]/g, ',')
The /g flag is to replace all the occurances within the string.
Upvotes: 4