Used in this manner, the ? indicates a non-greedy matching.
A typical greedy match matches as much as possible.
So this regex: .*b
applied against this text:
aaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
matches on the whole string:
aaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
But: .*?b matches as little as possible, so it matches on:
aaaaaaaaaaaaaaaaaaaaaaaaaab
stopping as soon as the match can be completed.
Your ([\s\S]*) regex matches on all characters, so the ? modifies the regex to look for the shortest number of characters that allows the regex to complete the match (i.e. end with your closing tag)