Skip to main content

dvlp.haus

SVG to base64

Convert SVG markup to Base64. Handy to convert SVGs to CSS.

SVG Preview

About

SVG to base64 uses regex to encode the provided SVG input.

const encodeSvg = (data: string) => {
  const symbols1 = /[\r\n%#()<>?\[\\\]^`{|}]/g

  data = data.replace(/'/g, '"')
  data = data.replace(/>\s{1,}</g, '><')
  data = data.replace(/\s{2,}/g, ' ')

  return data.replace(symbols, encodeURIComponent)
}

The SVG input is purified at runtime using DOMPurify before being injected into the DOM as a live preview.