我正在嘗試替換檔案中的字串,如下所示:
c:\>powershell -Command (gc D:\xxxxxxx\index.html) ^
-replace '^<script src="js/locale/languageholder.js"^>^</script^>', '' ^
| Out-File -encoding ASCII "D:\yyyyyyyyy\index.html"
我要洗掉的字串是:
<script src="js/locale/languageholder.js"></script>
沒有錯誤,但它只是沒有被替換。我認為必須有一些字符要轉義,但無法弄清楚。
代碼中的^用來轉義<,否則會報這個錯誤:
< was unexpected at this time.
使用 \ 轉義得到相同的錯誤
uj5u.com熱心網友回復:
\"在 PowerShell CLI 在命令列決議期間剝離未轉義 "字符(如果有)之后,您需要對要傳遞給最終執行的 PowerShell 命令的所有字符進行轉義:
powershell -Command (gc D:\xxxxxxx\index.html) ^
-replace '^<script src=\"js/locale/languageholder.js\"^>^</script^>', '' ^
| Out-File -encoding ASCII \"D:\yyyyyyyyy\index.html\"
有關更多資訊,請參閱此答案。
uj5u.com熱心網友回復:
最簡單的方法是擁有一個外部 powershell 檔案:
替換.ps1:
(gc index.html) -replace '<script src="js/locale/languageholder.js"></script>' |
set-content index2.html
然后運行:
powershell -file replace.ps1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418467.html
標籤:
