I'm parsing some fields out of a string, and I'm having a bit of difficulty getting the extraction correct.
My string can be formatted as such:
const myString = 'field1=field1_value;field2=field2_value'
Or
const myString = 'field1=field1_value'
In this example below, I'm trying to extract field1 and field2 out of the first string.
I've done something like:
const field1 = myString.match(/field1=(.*)(;)?/);
const field2 = myString.match(/field2=(.*)(;)?/);
The problem with this, is that for field1, it will match all the way from the beginning until the end of the string, including all the other fields. If I make the delimiter a mandatory match, then field2 doesn't match anything, as the last field in the string won't have any delimiters. I want this extraction to happen in a way that if a delimiter is present, it should only match up to that and since the last field doesn't have it, or if it is a single field without the delimiter, it should still extract it.
How can I form my regex to do this type of extraction?