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

171
Views
How to replace all occurrences by regex

I have a package.json and some of the dependencies versions are:

...
"dependencies": {
    "@octopol/common": "workspace:packages/common",
  }
...

I want to replace all these (workspace:packages/<something>) versions with, for example, 1.0.0.

I tried this:

`"@octopol/common": "workspace:packages/common","@octopol/aa": "workspace:packages/aa",`
.replace(/(workspace:packages\/(.*))/g, "1.0.0")

I was expecting to get:

"@octopol/common": "1.0.0","@octopol/aa": "1.0.0",

But the result was:

'"@octopol/common": "1.0.0'

What am I missing here?

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

0

The .* matches till the end of the line, and since your input is a single line, /(workspace:packages\/(.*))/g matches workspace:packages/ and then any zero or more chars other than line break chars, as many as possible, till the end of the line. You need to temper the dot in some way.

A possible solution can look like

console.log(
    `"@octopol/common": "workspace:packages/common","@octopol/aa": "workspace:packages/aa",`
.replace(/workspace:packages\/[^"]*/g, "1.0.0")
);

where workspace:packages\/[^"]* matches

  • workspace:packages\/ - workspace:packages/ string
  • [^"]* - zero or more chars other than ".

If the part to be replaced cannot contain whitespace, you can further precise the pattern as /workspace:packages\/[^"\s]*/g.

See the regex demo.

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!