I believe it's a simple question, but I haven't find a solution and spent a few hours trying to figure up why it's not working.
In a variable I have a string: _webconfig = new Object();
and I want to insert a line after it some text.
I tried to do it with using -replace but it don't work ok, because the function will be 'hitting' more than one time, resulting with the pattern x2.
This is the code I am using now (which don't work) because of the ; in the end of the $pattern variable:
$Pattern = "_webconfig = new Object();"
$replaceWith = "$Pattern `n_webconfig.$key = `"$value`";"
# write-Host "Pattern: $Pattern, replaceWith: $replaceWith"
$text= $text -replace $pattern, $replaceWith
$text at the end will be js file, and it needs to have ; at the end of each line.the result I want to have is:
_webconfig = new Object();
will become:
_webconfig = new Object();
_webconfig.env = "dev";
_webconfig.ServicesAppName= "Services";
How can I achieve that?
Probably all you have to do is to escape your regex string because of the two parentheses:
$Pattern = "_webconfig = new Object();"
$replaceWith = "$Pattern `n_webconfig.$key = `"$value`";"
$text= $text -replace [regex]::Escape($pattern), $replaceWith
If key and value are just strings then use String.Replace:
$Pattern = "_webconfig = new Object();"
$replaceWith = "$Pattern `n_webconfig.$key = `"$value`";"
# write-Host "Pattern: $Pattern, replaceWith: $replaceWith"
$text= $text.Replace($pattern, $replaceWith)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With