I am trying to remove a set of given values from a string of comma separated values as follows:
// mainString
mainString = '1,2,3,4,5'
// Values to subtracted from mainString
givenValuesString1 = '1'
givenValuesString2 = '2,3'
givenValuesString3 = '5'
givenValuesString4 = '3,5'
// Desired Output Values
desiredOutput1 = '2,3,4,5'
desiredOutput2 = '1,4,5'
desiredOutput3 = '1,2,3,4'
desiredOutput4 = '1,2,4'
What I tried is this:
desiredOutput = mainString.replace(givenValuesString, '')
desiredOutput = mainString.replace(`${givenValuesString},`, '')
My problem is that when I try the second method, value without a comma after it is not deleted i.e., the last character in the mainString. Another problem is I am not able to delete the values if they are one after another i.e, values like 2,4 or 3,5.
Values remaining if I use the above two methods:
// First method
desiredOutput1 = ',2,3,4,5'
desiredOutput2 = '1,,4,5'
desiredOutput3 = '1,2,3,4,'
desiredOutput4 = '1,2,3,4,5'
// Second method
desiredOutput1 = '2,3,4,5'
desiredOutput2 = '1,4,5'
desiredOutput3 = '1,2,3,4,5'
desiredOutput4 = '1,2,3,4,5'
So how can I achieve the desired result? i.e., the given values along with the comma must be deleted from the mainString and also even if they are not adjacent to each other.