Consider the following function arguments (they are already extracted of the function):
Monkey,'Blue Monkey', 'Red, blue and \'Green'', 'Red, blue and 'Green\''
Is there a way to extract arguments to get the following array ouput using regexp and stripping white spaces:
[Monkey, 'Blue Monkey', 'Red, blue and \'Green'', 'Red, blue and 'Green\'']
I’m stuck using this RegExp which is not permisive enough:
/(('[^']+'|[^\s,]+))/g
This looks a little nasty but it works:
I used
\x5Cinstead of the plain backslash character\as too much of those can be confusing.This regular expression consists of the parts:
'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'matches double quoted string declarations'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'matches single quoted string declarations[^'',]+matches anything else (except commas).The parts of
'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'are:[^\x5C']+matches anything except the backspace and quote character\x5C(?:\x5C\x5C)*[\x5C']matches proper escape sequences like\',\\,\\\',\\\\, etc.