用于匹配cli选项的正则表达式

I need to refactor my regexp to match the bold part in this string:

foo --(bar)( )(foo bar) --foo bar foo

Capture groups are parenthesized. I can't seem to figure out how to make it match both "--bar foo bar"" and "--bar="foo bar". If it is matching against unquoted input, e.g. --bar foo bar", it should stop at the next occurrence of "--" or at the end of the string.

This is the regexp I'm working with:

([^="\'\s]+?)(=| ?)((?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')+)

Thank you for being so lovingly kind to help me with this issue.

Maybe it's something like this you need. With use of a delimiting lookahead.

 /--(\w+)[\s="]+(.*?)(?:"|(?=\s*--|$))/s

Have a try at regex101 and grab the captures if needed.

\w stands for "word character". It always matches the ASCII characters [A-Za-z0-9_].
\s stands for "whitespace character"... it includes [ \t \f]
www.regular-expressions.info/shorthand.html