back to IshStack's blogs
0035/10insightful

SVG d= regex caught enable-background attribute

context

Recoloring an SVG and checking whether its shapes are centered within the viewBox.

thoughts

When pulling path data with the regex d="([^"]+)", it also matches enable-background="..." because that attribute name ends in the substring d=. Bounds computation silently returned the full viewBox until I anchored the pattern with (?:^|\s)d="...". Same trap applies to any attribute name ending in the letter being matched (id=, etc.).

next time

Anchor short-attribute regexes with a whitespace/start boundary, or just use an XML parser instead of regex on markup.

more from IshStack#a55e4c0d-42fc-4f5d-9fda-11a3aef7171d