How can I remove last * from each "organisations": value from the string without using JSON.parse() and remove last , from [] using javascript?

Please not the values inside the string are all dynamic.



let abc = '[{"userid":"88623619","organisations":"Imperial College London*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Milton Keynes Hospital NHS Foundation Trust*","oversightGroupRole":"Chair","oversightGroupMemberName":"Dr Ahmed Ahmed","oversightGroupType":"Steering Committee"},{"userid":"88624025","organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust*","oversightGroupRole":"Public Member","oversightGroupMemberName":"Professor b b","oversightGroupType":"Advisory Group"},{"userid":"88623985","organisations":"Aberdeen And District Fibromyalgia Support Group*Additive Design Ltd*Altnagelvin Area Hospital*Ataxia UK*Tameside Hospital NHS Foundation Trust*","oversightGroupRole":"Chair","oversightGroupMemberName":"Chief TestTestRr TestTestRr","oversightGroupType":"Data Monitoring Committee"},]';

//the Desried Result is

//abc = '[{"userid":"88623619","organisations":"Imperial College London*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Milton Keynes Hospital NHS Foundation Trust","oversightGroupRole":"Chair","oversightGroupMemberName":"Dr Ahmed Ahmed","oversightGroupType":"Steering Committee"},{"userid":"88624025","organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust","oversightGroupRole":"Public Member","oversightGroupMemberName":"Professor b b","oversightGroupType":"Advisory Group"},{"userid":"88623985","organisations":"Aberdeen And District Fibromyalgia Support Group*Additive Design Ltd*Altnagelvin Area Hospital*Ataxia UK*Tameside Hospital NHS Foundation Trust","oversightGroupRole":"Chair","oversightGroupMemberName":"Chief TestTestRr TestTestRr","oversightGroupType":"Data Monitoring Committee"},]';
My Code which is not working:
let abc = '[{"userid":"88623619","organisations":"Imperial College London*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Imperial College Healthcare NHS Trust*Milton Keynes Hospital NHS Foundation Trust*","oversightGroupRole":"Chair","oversightGroupMemberName":"Dr Ahmed Ahmed","oversightGroupType":"Steering Committee"},{"userid":"88624025","organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust*","oversightGroupRole":"Public Member","oversightGroupMemberName":"Professor b b","oversightGroupType":"Advisory Group"},{"userid":"88623985","organisations":"Aberdeen And District Fibromyalgia Support Group*Additive Design Ltd*Altnagelvin Area Hospital*Ataxia UK*Tameside Hospital NHS Foundation Trust*","oversightGroupRole":"Chair","oversightGroupMemberName":"Chief TestTestRr TestTestRr","oversightGroupType":"Data Monitoring Committee"},]';

    abc = abc.replace(/,(?=[^,]*$)/, '');

    let startIndex = 0;
    while ((startIndex = abc.indexOf('"organisations":', startIndex)) !== -1) {   
        let endIndex = abc.indexOf('*', startIndex + '"organisations":'.length);
        if (endIndex !== -1) {       
            abc = abc.slice(0, endIndex) + abc.slice(endIndex + 1);
        }
        startIndex += '"organisations":'.length;
    }

    console.log(abc);


Well ABC is just a string. If you for…whatever reason… dont want to use JSON to manipulate…a…JSON… (I’m gonna let that sentence hang for a minute, so that you think about it.)

You would have to handle the regex as a string replacement.

So your regex would have to be something like… organisations":"(.*?\*)([^*]*?\*)"/g and you’d replace it with organisations":"$1"

EDIT: Oh sorry, you literally just want to strip the last * off? I thought you meant the last entry.
/organisations":"(.*?)\*"/g , organisations":"$1"

when I use the following statement it removes “organisations”: as well.
abc = abc.replace(/organisations":“(.*?)*”/g, “$1”);

I need to remove last * from each “organisations”: value

Well you didnt use what i told you do, did you?

1 Like

It works. Txs all lot. Appreciated.

1 Like

Just to notice:

This is the most worse solution to find. No one should do it that way. Please use JSON.parse, change the data and stringing it after. That’s the only valid way…

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.