Home > Software engineering >  How to replace variable value in js file using powershell
How to replace variable value in js file using powershell

Time:02-25

I have a java script file called require.config.js

There is a variable in it called "waitSeconds" with a value of 15 and I want to replace the 15 with a 60 - using powershell. I know I could use replace to replace 15 with 60 - but I don't know that the value will always be 15. So I want to replace the value for "waitSeconds"

Here's an example of the file - want to change whatever the value for "waitSeconds" is (in this case 15) - to a different value -ie 60

require.config({

  "waitSeconds": 15,

  "unitTestUser": {

    "Username": "user",

    "Password": "password"

  },

  "paths": {

    "modules": "modules",

    "semantic-ui": "client/ui/semantic-ui/semantic",

CodePudding user response:

You could use Get-Content to read the file and -replace to replace that particular line and set the new value, for example:

$content = Get-Content path/to/require.config.js -Raw
$content -replace '("waitSeconds":\s*)\d ,', '${1}60,' | Set-Content path/to/require.config.js
  • Related