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

Joined
Oct 24, 2013
Messages
47
Reaction score
0
Please not the values inside the string are all dynamic.

JavaScript:
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);
 
Last edited:
Joined
Jul 4, 2023
Messages
475
Reaction score
58
Did you consider to use RegExp (Regular expressions)
JavaScript:
const 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"}]';

// Find and replace the last '*' in each "organisations" value
let modifiedString = abc.replace(/(\*[^*]*)\*"/g, '$1"');
console.log(modifiedString);

  1. (\[^]*): This is a capturing group that consists of:
    • \: Matches a literal ''.
    • [^*]: Matches any sequence of characters ( excluded) - this is a negated character class denoted by [^*].
      This combination captures everything up to the last '' in the "organisations" value. The * in the character class [^*] ensures that we can match any character except ''.
  2. \": This part matches the last '' in the "organisations" value followed by a double quote ".
The g flag at the end of the regular expression (/(\[^])\"/g) enables the global flag, which allows the regular expression to match all occurrences in the input string, not just the first one.

In the replacement string '$1"', $1 refers to the content captured by the first capturing group (\[^]). So, it effectively replaces the last '' in each "organisations" value with an empty string and adds a double quote (").

Putting it all together, this regular expression is designed to identify and replace the last *" in each "organisations" value in the input string, achieving the desired result of removing the trailing '' from each "organisations" value.
 
Last edited:
Joined
Oct 24, 2013
Messages
47
Reaction score
0
Did you consider to use RegExp (Regular expressions)
JavaScript:
const 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"}]';

// Find and replace the last '*' in each "organisations" value
let modifiedString = abc.replace(/(\*[^*]*)\*"/g, '$1"');
console.log(modifiedString);

  1. (\[^]*): This is a capturing group that consists of:
    • \: Matches a literal ''.
    • [^*]: Matches any sequence of characters ( excluded) - this is a negated character class denoted by [^*].
      This combination captures everything up to the last '' in the "organisations" value. The * in the character class [^*] ensures that we can match any character except ''.
  2. \": This part matches the last '' in the "organisations" value followed by a double quote ".
The g flag at the end of the regular expression (/(\[^])\"/g) enables the global flag, which allows the regular expression to match all occurrences in the input string, not just the first one.

In the replacement string '$1"', $1 refers to the content captured by the first capturing group (\[^]). So, it effectively replaces the last '' in each "organisations" value with an empty string and adds a double quote (").

Putting it all together, this regular expression is designed to identify and replace the last '' in each "organisations" value in the input string, achieving the desired result of removing the trailing '' from each "organisations" value.
Thank you for your response. Your provided solution inadvertently removes asterisks from other properties, which is not the desired behaviour. For instance, if the "userid" property contains asterisks, they are also removed.

e.g
"userid":"88623619*123*" then it removes * from there as well.
 
Joined
Jul 4, 2023
Messages
475
Reaction score
58
But you provided sample of data as in this image ...
1708099162122.png

userid did not looks like
1708099230097.png

next time you ask a question you need to be more precise ;)

Now you know how to work with RegExp, try changing the expression to omit numbers like the values in userid.
 
Last edited:
Joined
Oct 24, 2013
Messages
47
Reaction score
0
But you provided sample of data as in this image ...
View attachment 2782
userid did not looks like
View attachment 2783
next time you ask a question you need to be more precise ;)

Now you know how to work with RegExp, try changing the expression to omit numbers like the values in userid.
I did mention in my initial message that all values within the string are dynamic(Please not the values inside the string are all dynamic.) However, thank you for your assistance nonetheless.
 
Joined
Jul 4, 2023
Messages
475
Reaction score
58
Check this one ...
JavaScript:
const abc = '[{"userid":"88623619*111*","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*222*","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*4442*","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*"}]';

let modifiedString = abc.replace(/\*"/g, "\"");
console.log(modifiedString);

e.g
"userid":"88623619*111*" => "userid":"88623619*111"
"oversightGroupType":"Steering Committee*" => "oversightGroupType":"Steering Committee"
"organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust*" =>
"organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust"
 
Joined
Oct 24, 2013
Messages
47
Reaction score
0
Check this one ...
JavaScript:
const abc = '[{"userid":"88623619*111*","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*222*","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*4442*","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*"}]';

let modifiedString = abc.replace(/\*"/g, "\"");
console.log(modifiedString);

e.g
"userid":"88623619*111*" => "userid":"88623619*111"
"oversightGroupType":"Steering Committee*" => "oversightGroupType":"Steering Committee"
"organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust*" =>
"organisations":"Bangor University*Barnet, Enfield and Haringey Mental Health NHS Trust*Cambridge University Hospitals NHS Foundation Trust"
TXS a lot. Appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top