I’m comparing 2 URL query strings to see if they’re equal; however, I want to ignore a specific query parameter (always with a numeric value) if it exists. So, these 2 query strings should be equal:
firstName=bobby&lastName=tables¶mToIgnore=2
firstName=bobby&lastName=tables¶mToIgnore=5
So, I tried to use a regex replace using the REReplaceNoCase function:
REReplaceNoCase(myQueryString, "¶mToIgnore=[0-9]*", "")
This works fine for the above example. I apply the replace to both strings and then compare. The problem is that I can’t be sure that the param will be the last one in the string… the following 2 query strings should also be equal:
firstName=bobby&lastName=tables¶mToIgnore=2
paramToIgnore=5&firstName=bobby&lastName=tables
So, I changed the regex to make the preceding ampersand optional… "&?paramToIgnore=[0-9]*". But – these strings will still not be equal as I’ll be left with an extra ampersand in one of the strings but not the other:
firstName=bobby&lastName=tables
&firstName=bobby&lastName=tables
Similarly, I can’t just remove preceding and following ampersands ("&?paramToIgnore=[0-9]*&?") as if the query param is in the middle of the string I’ll strip one ampersand too many in one string and not the other – e.g.
firstName=bobby&lastName=tables¶mToIgnore=2
firstName=bobby¶mToIgnore=5&lastName=tables
will become
firstName=bobby&lastName=tables
firstName=bobbylastName=tables
I can’t seem to get my head around the logic of this… Can anyone help me out with a solution?
If you can’t be sure of the order the parameters appear i would recommend, that you don’t compare them by the string itsself.
I recommend splitting the string up like this:
Then go through arrays and make the paramToIgnore somehow euqal:
Then you can sort and compare the arrays to see if they are equal:
I’m pretty sure it’s possible to make this more compact and give it a better performance. But with comparing strings like you do, you somehow alsways have to care about the order of your parameters.