0

I am trying to replace the version number in a couple of files using the package.json file and "replace": "1.1.0" by running an npm script. I am running into a Regex issue I believe with the replace. I think maybe the solution should be some kind of regex grouping where I just replace the .* value with just $npm_package_version = solved, but I don't know how to do that.

Package.json version = "version": "2.0.45",

Strings to find = "ApiVersion": "Any number and periods like this 1.2.67",

Code Attempted = "updateVersion": "replace \"\"ApiVersion\": .*\",\" \"\"ApiVersion\": \"$npm_package_version\",\" ../appsettings.json",

Expected Outcome = in file appsetting.json the ApiVersion should be changed to = "ApiVersion": "2.0.45",

Error Received = "ApiVersion": "$npm_package_version",

I am assuming there is something going on here with regex, but I can't figure out how to get the npm package version var to render inside the string value. It works just as $npm_package_version.

RooksStrife
  • 1,647
  • 3
  • 22
  • 54

1 Answers1

0

finally got it. This is supppper useful in my book.

postversion": "replace '(?<=\"ApiVersion\":\\s\").*(?=\",)' $npm_package_version ../appsettings.json",

To explain the parathesis (text in here) creates a group. The ?<= creates a Lookbehind. The \\s match any whitespace - double escaped. The .* matches all in between the two-match requirements. The ?= creates a look ahead. So Look/Match step through = "ApiVersion": any space " anything between ", replace only the in-between with the npm variable.

I think that is close to what it really does - not a regex expert.

Got help here -How to replace captured groups only?

RooksStrife
  • 1,647
  • 3
  • 22
  • 54