Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!