Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Powershell中替换文本字符串?_Powershell - Fatal编程技术网

如何在Powershell中替换文本字符串?

如何在Powershell中替换文本字符串?,powershell,Powershell,我有一行代码似乎无法正确反斜杠: (Get Content prefs.js){$\替换“Mozilla/5.0(兼容;Googlebot/2.1;+)”,“Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/29.0.1547.2 Safari/537.36”}设置内容prefs.js看起来您处理的是文字字符串。不要使用-replace操作器 它处理正则表达式。使用Replace方法: ... | %

我有一行代码似乎无法正确反斜杠:


(Get Content prefs.js){$\替换“Mozilla/5.0(兼容;Googlebot/2.1;+)”,“Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/29.0.1547.2 Safari/537.36”}设置内容prefs.js
看起来您处理的是文字字符串。不要使用
-replace
操作器 它处理正则表达式。使用
Replace
方法:

... | %{$_.Replace("string to replace", "replacement")} | ...
或者,如果仍要使用
-replace
,则也可以使用
[regex]::Escape()
。它会帮你逃走的

示例:将文本逐字替换为“$”

比较以下结果,显示如果在正则表达式替换中使用自动变量可能发生的情况:

[PS]> "Hello" -replace 'll','$_'                  # Doesn't work!
HeHelloo

[PS]> "Hello".Replace('ll','$_')                  # WORKS!
He$_o

谢谢,这解决了我的问题,使用的方法,而不是正则表达式之一!