I need help with creating a regex pattern to split a string of words in a specific way.
The current pattern I've been using is (?!\(.*)\s(?![^(]*?\))
, but it's not giving me the desired outcome.
It's close, but not quite there yet.
let message = 'The quick brown fox (and friend) (jumps over the) lazy dog.'
let result = message.match(/regex/g);
Result:
[
'The',
'quick',
' ',
'brown',
' ',
'fox',
' ',
'(and friend)',
' ',
'(jumps over ( bonus regex - only captures the most outer parent parentheses) the)',
' ',
'lazy',
' ',
'dog.'
];
Special attention should be given to preserving white space and treating long sentences within parentheses as single array items.
Despite finding similar questions, I struggle to adjust the answers to fit my needs due to variations in regex solutions. I'm really bad at regex overall.
Any assistance on this matter would be greatly appreciated!
Edit - Remembered another scenario where consecutive parentheses are present in the string.