Powershell无法扩展此javascript内容 [英] Powershell cannot expand this javascript content
问题描述
将答案应用于我之前的问题如何使用powershell扩展文件内容
Applying the answer to my previous question How to expand file content with powershell
我试图扩大这个时偶然发现致命错误:
I stumbled upon a fatal error when trying to expand this :
test.js:
<script type="text/javascript">
$('.mylink').click(function(event) {
var hash = $(this).attr("href");
});
// $var
</script>
test.ps1
$test = get-content -raw test.html
$var = "test"
# Write to output file using UTF-8 encoding *without a BOM*.
[IO.File]::WriteAllText(
"$PWD/out.html",
$ExecutionContext.InvokeCommand.ExpandString($test)
)
推荐答案
$
是一个PowerShell中的特殊符号和 $()
表示扩展了内容的内联表达式,因此您需要在扩展之前通过添加反引号来转义它。
之前 $(
:
$
is a special symbol in PowerShell and $()
denotes an inlined expression expanded with its contents so you'll need to escape it before expansion by adding a backtick `
before $(
:
$ExecutionContext.InvokeCommand.ExpandString(($test -replace '\$\(', '`$('))
但是,如果你在模板中使用PowerShell的 $()
,那么这个问题需要更智能的替换,可能需要更复杂的算法来区分jQuery和PS。
However, if you use PowerShell's $()
in the template then it's a problem that would require a smarter replacement and probably much more complex algorithm to differentiate between jQuery and PS.
这篇关于Powershell无法扩展此javascript内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!