Can you explain the semantics of following regex? Especially, what ?: and () means?
/(?:^|:|,)(?:\s*\[)+/g
Thanks.
?: means a non-matching group. Usually () saves a reference for replacements.
So "abc".replace(/.(.)./, "x$1x") would return "xbx"
Then adding ?: treats them just as groups but not saving them for later.
Typical usage in html regex would be something like tag <.*(?:href|src)='"['"]
This looks for href or src attributes and then saves the value.
With the brackets () you can group some stuff together, e.g. for (?:\s*\[)+ the quantifier + (one or more) belongs to the whole group \s*\[, that means it will match more than one [ with optional whitespace before each square bracket.
Those groups are per default capturing groups, that means the matched part is put into a variable and can be reused using backreferences, or just as result.
This default behaviour can be changed by putting ?: after the opening bracket, so (?:\s*\[)+ is a non-capturing group, i.e. the matched part is not stored somewhere.
() tells the parser to capture the match so you can reference it.
?: inside the () tells the parser not to capture the match
This is the whole explanation:
Match the regular expression below «(?:^|:|,)»
Match either the regular expression below (attempting the next alternative only if this one fails) «^»
Assert position at the beginning of the string «^»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «:»
Match the character “:” literally «:»
Or match regular expression number 3 below (the entire group fails if this one fails to match) «,»
Match the character “,” literally «,»
Match the regular expression below «(?:\s*\[)+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “[” literally «\[»
Read Regular Expression Advanced Syntax Reference for an explanation on this and other regex syntactic explanations.