Commas are most probably the tiniest creature that can cause heartbreaks to any copy editor. The simple reason is that they are not solely governed by rules. Arguably, using commas have as many exceptions as there are rules.

Consider such as for example.

The such as phrase may, or may not, take commas based on the context. Compare these two phrases:

Animals such as lion and tiger are ferocious predators.

Wild animals, such as lion and tiger, are generally ferocious.

In the first example, the phrase such as lion and tiger acts as a restrictive phrase, identifying the characteristics of Animals. However, in the second sentence, it only serves as an example, hence not essential for the meaning of the sentence. So the phrase is parenthetical in the second example and requires commas to separate them.

Finding out whether the such as phrase is essential or not follows the same logic for identifying relative clauses as restrictive or nonrestrictive: lift off the phrase and see whether the sentence conveys the same meaning. If so, the phrase is parenthetical and needs comma(s). Else, the phrase is defining and no commas to be used.