0

I am using powershell script to replace some content of the text which is in a javascript file

 var i = {
        production: !0,
        myBaseURL: "https://www.sample.com/api/",
       }

 (Get-Content -literalPath $pathToJsFile -raw) -replace "https://www.sample.com", 
  "https://www.newserver.com"|

    Out-File $jspath -encoding UTF8

I want to replace this using regex as it can be anything instead of "https://www.sample.com" I tried

 $file -replace "myBaseURL\s*: .*", "myBaseURL: $([char]34)https://www.newserver.com([char]34),"

is there a cleaner way to do this ?

Rohit
  • 10,056
  • 7
  • 50
  • 82

1 Answers1

0

This is generally a bad idea. Instead of replacing, consider generating a JS file with the attributes, and including it earlier than your code in the DOM.

Alternatively use your domain name to decide which environment you are in.

var env;
switch(window.location.hostname){
  case "example.com": 
    env = "prod"; 
  case "dev.example.com":
    env = "dev"; 
John Pavek
  • 2,595
  • 3
  • 15
  • 33