我正在處理由 Acrobat 創建的 HTML 檔案,它不使用正確的 HTML 物體來轉義 Unicode 字符。我需要在正則運算式模式中包含單引號和雙引號,但是我為轉義這些字符所做的每一次嘗試都在我的腳本中失敗了......即使它在常規 PowerShell 會話中作業。
例如,此查找/替換不起作用:
$html = $html.Replace("`“", '“')
$html = $html.Replace("`”", '”')
$html = $html.Replace("`‘", '‘')
$html = $html.Replace("`’", '’')
...但如果我闖入我的腳本并從除錯提示運行這些替換行之一,它確實有效。
編輯:這是我現在正在測驗的標記片段:
<p style="padding-left: 5pt;text-indent: 17pt;line-height: 119%;text-align: justify;">To guide its readers the Hermetica makes use of the mystical astrological world-view that we have been discussing. It describes the creation of the world as a series of emanations, starting with the Light, who gave birth to a son called Logos. In the words of Hermes’s guide, Poimandres:</p><p style="padding-left: 24pt;text-indent: 0pt;line-height: 119%;text-align: justify;">“That Light,” he said, “is I, even Mind, the first God, who was before the watery substance which appeared out of the darkness; and the Logos which came forth the Light is son of God.”</p><p style="padding-left: 21pt;text-indent: 1pt;line-height: 119%;text-align: justify;">(Scott, Walter, translator, Hermetica: The Ancient Greek and Latin Writings Which Contain Religious or Philosophical Teachings Ascribed to Hermes Trismegistus, Boston: Shambhala: 1985, p. 117)</p>
如果$html等于該字串,則我查找和替換字符的嘗試似乎是徒勞的。
uj5u.com熱心網友回復:
嘗試使用 Unicode 值而不是反參考文字:
$html = $html.Replace("`u{201C}", '“')
$html = $html.Replace("`u{201D}", '”')
$html = $html.Replace("`u{2018}", '‘')
$html = $html.Replace("`u{2019}", '’')
生產

如果您在編碼方面遇到問題(例如您建議的 UTF-8),請查看https://unicode-table.com - 您可以獲得任何編碼的代碼值。
uj5u.com熱心網友回復:
顯然,PowerShell 使用非 BOM UTF-8 編碼做了一些有趣的事情。將 VSCode 設定為使用 BOM將PowerShell 腳本自動編碼為 UTF-8允許 String.Replace 函式按預期運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372234.html
標籤:电源外壳
