1

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?

2 Answers 2

4

Try regular expression

dragdropMatchResponseData.replace(/\[,\]/g, ':').replace(/\[:\]/g, ',')

The /g flag is to replace all the occurances within the string.

Sign up to request clarification or add additional context in comments.

Comments

0

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, ':'))

Comments

Your Answer

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