2

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?

1
  • "@octopol/common": "workspace:packages/common","@octopol/aa": "workspace:packages/aa", .replace(/(?!\")(workspace:packages\/(.*?)(?=\"))/g, "1.0.0"); regex101.com/r/DxCUlt/1 Commented Nov 9, 2021 at 10:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.