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?
CodePudding user response:
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")
);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
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.