Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
Convert CSS Declaration into a Javascript Object

I've written a script that extracts the @font-face declaration from CSS, and I'd now like to convert it into a javascript object. Here's an example of what the original string will look like:

{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}

Thats the string I'd like to convert into an object ^.

I tried using JSON.parse(), but it doesn't work since it's not a JSON encoded string. Is therea simple way that I'm missing to convert this string into a javascript object, or will I have to loop through it and manually convert it?

Any help is appreciated — thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The key point is using regex to convert CSS to JSON format and then using JSON.parse. A description of each replace part is included in the code.

var css = '{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}';

cssObject = JSON.parse(css
  .replace(/"/g, '\\"')               // add \ before "
  .replace(/\s*\{\s*/, '{"')          // add start "
  .replace(/\s*;?\s*\}\s*/, '"}')     // add end "
  .replace(/\s*:\s*/g, '":"')         // replace : with ":"
  .replace(/\s*;\s*/g, '","')         // replace ; with ","
);

console.log("CSS Object: ", cssObject);

To convert keys to camel format another replace is required as the following code:

var css = '{font-family:GT Alpina;font-weight:100;src:url(/next/contenthash/next/fonts/gt-alpina/thin/GT-Alpina-Standard-Thin.contenthash.3322f75174e682b58d25cca4c516f38b51edb6ef.woff2) format("woff2");font-display:swap}';

cssObject = JSON.parse(css
.replace(/"/g, '\\"')               // add \ before "
  .replace(/\s*\{\s*/, '{"')          // add start "
  .replace(/\s*;?\s*\}\s*/, '"}')     // add end "
  .replace(/\s*:\s*/g, '":"')         // replace : with ":"
  .replace(/\s*;\s*/g, '","')         // replace ; with ","
  .replace(/"([^"]+)":/g, (full, property) => '"' + property.toLowerCase().replace(/-(\w)/, (a, b) => b.toUpperCase()) + '":')
);

console.log("CSS Object with Camel Format: ", cssObject);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda