Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
javascript capture css rule block in string

How to capture css rule block in a string? This is too hard for me to capture, without the new line

.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }

What I want to accomplish is to get each those block into array

['.fa { color: red; }', 'small { font-size: 16px; }', '#content { background-color: blue; }']

Is this possible in javascript regex?

UPDATE

Sorry guys, I Thought this is only solvable with regex, But got an answer which is way simpler, Thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Split by a closing curly brace:

const str = `.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }
`

const res = str.split("}").map(e => e += "}")
console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can match any sequence of characters that aren't a closing curly brace (}) up to the curly brace, as many times as there are. You might want to remove the newlines first, e.g.

let s = `.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }`;

console.log(s.match(/[^}]*}/g));

Of course malformed rules will cause an issue.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const css = `.fa { color: red; }
             small { font-size: 16px; }
             #content { background-color: blue; }`;

 const splitCSS = css.split(/\n/g);

there's a much simpler way...

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda