[英]Powershell cannot expand this javascript content
Applying the answer to my previous question How to expand file content with powershell 将答案应用于我之前的问题如何使用Powershell扩展文件内容
I stumbled upon a fatal error when trying to expand this : 我在尝试扩展此错误时偶然发现了一个致命错误:
test.js: test.js:
<script type="text/javascript">
$('.mylink').click(function(event) {
var hash = $(this).attr("href");
});
// $var
</script>
test.ps1 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)
)
$
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 $(
: $
是PowerShell中的特殊符号,而$()
表示一个内联表达式,其内容已扩展,因此您需要在扩展前通过在$(
之前添加反引号`
对其进行转义。
$ExecutionContext.InvokeCommand.ExpandString(($test -replace '\$\(', '`$('))
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的
$()
,那么这将是一个问题,需要更智能的替换,并且可能需要更复杂的算法来区分jQuery和PS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.