I have about 20 react projects, all of which have package.json files. I have packages like this in them:
"@material-ui/core": "4.11.4", "@material-ui/icons": "4.11.2", "@material-ui/lab": "4.0.0-alpha.58", "@material-ui/styles": "4.11.4",
I need to go through every single one line and update them to “0.0.0”. Is there a regex that can match "***.***.***"
and replace it with “0.0.0” ?
Reason why I need to do this: I create my own packages. I moved them from one location to another, so their versions started from 0 all over again. If I do npm check updates, and package is 5.8.34 for example, it won’t update it back to 0.0.1. But if I update all of their values to 0.0.0 then run npm check updates it’ll update to 0.0.1.
Also, why anyone would be voting -1 here? Don’t like the question or don’t know the answer? I provided all I need to accomplish here and Below are some Regex I attempted. Why would you still vote -1?
I tried:
: "[0-9].+?"
So far this does the best job
: "[0-9].[0-9].[0-9]"
It won’t find ones that have letters in package name such as "@material-ui/lab": "4.0.0-alpha.58"
. If anyone shares a better solution I’ll update the question.
This is purely a question how to reset versions of Packages in Package.json file to 0.0.0.
Advertisement
Answer
If all the packages start with a digit followed by a dot and word characters which can optionally have a hyphen in between the word characters, you can use a capture group and use that in the replacement.
Pattern:
("[^s"]+":s*")d+(?:.w+(?:-w+)*)+",
The pattern matches:
(
Capture group 1 (denoted by$1
in the example code)"[^s"]+":s*"
Match from"..."
then:
and optional whitespace chars
)
Close group 1d+
Match 1+ digits(?:
Non capture group.w+(?:-w+)*
Match a.
1+ word chars and optionally-
and 1+ word chars
)+
Close the non capture group and repeat 1+ times",
Match a double quote and comma
Replace with
$10.0.0",
const regex = /("[^s"]+":s*")d+(?:.w+(?:-w+)*)+",/; [ `"@material-ui/core": "4.11.4",`, `"@material-ui/icons": "4.11.2",`, `"@material-ui/lab": "4.0.0-alpha.58",`, `"@material-ui/styles": "4.11.4",` ].forEach(s => console.log(s.replace(regex, `$10.0.0",`)));