在正向后方使用可选组

男孩,我在正则表达式上很烂。

I need to match newlines and whitespaces between IMG BBCodes in series so I can replace them with single whitespace. But the problem is they also could be wrapped with URL tags (so this means the URL tag is optional). Here are some possible combinations:

[/img]    [img ...]

[/img][/url]
     [url ...][img ...]

[/img]     [url ...][img ...]

[/img][/url]    
    [img ...]

我走了这么远:

(?:\[\/img\](?:\[\/url\])?)([\s]+)(?=(?:\[url(?:.+?)?\])?\[img(?:.+?)?\])

Sadly, it also matches BBCodes which I don't want to. Please check it on regex101 (I want green ones - Group 1 only).

I can't use positive lookbehind because it says "A quantifier inside a lookbehind makes it non-fixed" when I try to make URL tag optional. See my attempt here.

我现在该怎么办?