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

177
Views
regex remove line breaks between double quotes without affecting other line break (eager match)

I have a map (key, value pairs) and few of these values have line breaks within them (inside double quotes) which is causing issues in CSS.

How can I remove these link breaks (the ones inside double quotes only) using regex find and replace (suitable in JavaScript).

Input:

  actionIconTransition: " background-color 0.2s,
  color 0.2s, box-shadow 0.2s",
  listItemTransition:
    " background-color 0.2s,
  border-color 0.2s, box-shadow 0.2s",
  primeIconFontSize: " 1rem",
  divider: " 1px solid #dee2e6",

Expected output:

  actionIconTransition: " background-color 0.2s,color 0.2s, box-shadow 0.2s",
  listItemTransition:
    " background-color 0.2s,border-color 0.2s, box-shadow 0.2s",
  primeIconFontSize: " 1rem",
  divider: " 1px solid #dee2e6",

I have tried many regexes but they are either removing line breaks in last two lines too, or don't do anything at all.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I found the answer. Adding it so that newbies get benefited.

The following regex select the line breaks only inside the double quotes or single quotes without affecting other lines

Find: [^"'],\n

Replace: ,

Javascript code for doing the same

.replace(/[^"'],\n/gm,",");

The flags "gm" above is optional. Use it if your strings span across multiple lines and there are multiple occurences for the given pattern which needs to be replaced.

Note: This answer is useful for my usecase. If anyone faces slightly different problem I can help with a regex with their usecase.

about 4 years ago · Juan Pablo Isaza Report

0

Use

let test_str = `actionIconTransition: " background-color 0.2s,
  color 0.2s, box-shadow 0.2s",
  listItemTransition:
    " background-color 0.2s,
  border-color 0.2s, box-shadow 0.2s",
  primeIconFontSize: " 1rem",
  divider: " 1px solid #dee2e6",`
console.log(test_str.replace(/"[^"]*"|'[^']*'/g, (z) => z.replace(/[\r\n]+/g, '')))

I.e. remove carriage return and line feed characters between quotes only.

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!