PowerShell:-用前导 $ 符号替换变量中的单词
PowerShell: -replace words in variable with leading $ symbol
我的objective:
我希望通过替换字符串中某些关键字的函数来更改字符串变量。
问题: 需要替换的关键字由前导 $ 标识。
我的代码:
function ReplaceStuff{
param([string]$text=$(throw 'text is required.'))
$text -replace "`$uid" , $UserId
}
注意:ReplaceStuff 函数在主脚本引用的另一个 .ps1 文件中,但我认为这与问题无关,因为没有 $ 符号工作正常
$mainText = ReplaceStuff($mainText)
echo $mainText
OUTPUT:(与原始变量中的文本没有区别)
blah blah blah $uid
我一直在网上搜索。虽然我发现了类似的问题,但实际上对我没有任何帮助。我发现最接近我的确切问题的是在 $ 上添加勾号 (`),但它对我不起作用。
只需使用单引号,使字符串成为文字并使用反斜杠转义 $ 正则表达式标记:
$text -replace '$uid' , $UserId
上一个问题:Passing string included dollar signs to -Replace Variable
我的objective: 我希望通过替换字符串中某些关键字的函数来更改字符串变量。
问题: 需要替换的关键字由前导 $ 标识。
我的代码:
function ReplaceStuff{
param([string]$text=$(throw 'text is required.'))
$text -replace "`$uid" , $UserId
}
注意:ReplaceStuff 函数在主脚本引用的另一个 .ps1 文件中,但我认为这与问题无关,因为没有 $ 符号工作正常
$mainText = ReplaceStuff($mainText)
echo $mainText
OUTPUT:(与原始变量中的文本没有区别)
blah blah blah $uid
我一直在网上搜索。虽然我发现了类似的问题,但实际上对我没有任何帮助。我发现最接近我的确切问题的是在 $ 上添加勾号 (`),但它对我不起作用。
只需使用单引号,使字符串成为文字并使用反斜杠转义 $ 正则表达式标记:
$text -replace '$uid' , $UserId
上一个问题:Passing string included dollar signs to -Replace Variable