Does removeAttrs support regular expression patterns for attribute matching?
0 reputation · 27 May 2020, 20:10 UTC
0 reputation · 27 May 2020, 20:10 UTC
The svgo removeAttrs plugin allows users to specify attributes for removal via the removeAttrs configuration option. While the documentation notes that users can provide attribute names or patterns, it does not explicitly state whether these patterns are treated as literal strings, support regular expression syntax, or observe case‑sensitivity rules.
Additionally, when multiple patterns are supplied, the precedence rules for overlapping or conflicting entries are not documented, nor is it clear whether users can define exclusion patterns to protect certain attributes from removal.
removeAttrs option accept regular expression patterns, and if so, what syntax and flags are supported?34775 reputation · 28 May 2020, 00:25 UTC
Yes, the removeAttrs plugin treats each entry in its attrs array as a JavaScript regular expression. Metacharacters such as (fill|stroke), .*, or ^data- are interpreted as regex patterns, not literal strings.
^pattern$). fill matches only an attribute named exactly fill; use .* explicitly for partial matches.: (default elemSeparator) into an element-name pattern and an attribute-name pattern, e.g., path:fill or (path|rect):(fill|stroke). Both halves receive the same regex treatment.Fill and fill are distinct.keep or exclude option exists. The only built-in protection is preserveCurrentColor, which retains fill/stroke when set to currentColor. Negation must be encoded inside the regex itself (e.g., negative lookahead), which is fragile due to the plugin's anchoring.SVGO v1, v2, and v3 differ in plugin API and option naming. The behavior described applies to the v2/v3 removeAttrs plugin. Confirm against your installed major version.
cat node_modules/svgo/plugins/removeAttrs.js | grep -n "new RegExp" (or the equivalent path for your version).test.svg with <rect fill="red" stroke="blue" data-custom="x"/>, then run svgo --config='{plugins:[{name:"removeAttrs",params:{attrs:["(fill|stroke)"]}}]}' test.svg -o - and verify both attributes are removed.Fill="green" to the SVG and confirm it survives the same pattern.Which SVGO major version are you running (svgo --version)? The option structure and default separator changed between v1 and v2+, so the exact config syntax depends on that answer.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.