I'm looking for a regex which can remove spaces when they are between matching brackets, i.e. ( and ), except when there are spaces inside quotation marks ( ' or ") inside the brackets.
I currently have the regex \s+(?=[^(]*\)), which removes all spaces found between brackets. So also when there are spaces in quotation marks.
// My input
do something with(in = 1, text='some text with spaces' , text2="also has spaces")
// My current output
do something with(in=1,text='sometextwithspaces',text2="alsohasspaces")
// My desired output
do something with(in=1,text='some text with spaces',text2="also has spaces")
Furthermore:
- Quotation marks can only be found inside the brackets
- There can be
"in''text:text='text with " inside', without the\escape character. - There can be
'in""text:text="text with ' inside", without the\escape character. - There is no escape character before the quotation mark in the string:
text='This is \" not there'
I know there are quite some questions about regex patterns, but I couldn't find one which did the trick. Among the many things that I've tried were look forwards to only find things in between ( until ' or " with \s+(?=[^("]*[\)"]), but that still finds the spaces in between " and ".
Can someone point me in the right direction?