2016-09-15 132 views
0

我正在使用powershell自动将代码行添加到某些脚本。看下面的例子:打破powershell中的替换字符串

$a -replace '<div class="ef-column1 bodyContent" id="column1">', '<div class="ef-column1 body-content" id="column1"> @RenderSection("ColumnMainHeader", false)' 

@RenderSection部分应该在一个新的行。所以我试图在@RenderSection前添加'n,但这会创建'n @ RenderSection,而不是将@RenderSection放在新行上。

我也尝试过'r'n @ RenderSection,但是这有同样的效果。把n放在“”之间也不会起作用。

+0

(https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/07/powertip-new-lines-with-powershell /) – BenH

+0

我正在使用坟墓' – 2hTu2

回答

2

问题在于前面的单引号使得坟墓变成了字面的而不是逃避'n。

尝试使用双引号,然后逃脱所有在表达式中的双引号:?您是否使用了单引号或剔]

$a -replace '<div class="ef-column1 bodyContent" id="column1">', "<div class=`"ef-column1 body-content`" id=`"column1`"> `[email protected](`"ColumnMainHeader`", false)" 
+0

这有帮助,谢谢 – 2hTu2