-replace不会用"()"替换字符串

Idr*_*ala 2 powershell

我正在尝试在PowerShell中替换包含括号的字符串.但是,当我尝试这样做时,它无法正常工作.

知道我哪里出错了吗?什么是我该做的更换包含字符串()-replace在PowerShell中?

$a='Some Text with (round) brackets. This text is long.'
$ch="with (round) brackets."
$b="this text"
$a -replace $ch,$b
Run Code Online (Sandbox Code Playgroud)

输出:

Some Text with (round) brackets. This text is long.
Run Code Online (Sandbox Code Playgroud)

Mar*_*ndl 6

-replace使用正则表达式,所以你必须逃避你的regex:

$a='Some Text with (round) brackets. This text is long.'
$ch="with (round) brackets."
$b="this text"
$a -replace [regex]::Escape($ch),$b
Run Code Online (Sandbox Code Playgroud)

输出:

Some Text this text This text is long.
Run Code Online (Sandbox Code Playgroud)