I'm looking to separate a string into an array made up of consecutive substrings, alternating between matches and non-matches of a specified regular expression.
Here's an example:
[nonmatch, match, nonmatch, match...]
For instance, using the correct regex (the exact expression isn't important for now),
"I [went to the doctor] today to [eat some] food"
could be transformed into:
["I ", "[went to the doctor]", " today to ", "[eat some]", " food"]
I have this requirement because I need to temporarily remove certain parts of a string, perform actions on the remaining text, and then place back the removed sections to reconstruct the original string (by simply joining all elements of the array).
All my searches so far lead me to solutions where individuals either want to eliminate certain portions of the input string (e.g. the [] in the above example) or merge non-matches and matches together, like:
["I ", "[went to the doctor] today to ", "[eat some] food"]