我想用 PowerShell 腳本調整 html 字串。級聯樣式表的 URL 應替換為本地 CSS 檔案。我將此替換命令與正則運算式一起使用。但結果并不如預期。
$a = '<link rel="stylesheet" type="text/css" href="https:/my.web.site/c5fb04c9-44e6-40d1-bb1f-46b170a9e53f" id="style-standard" />'
$b = $a.Replace('href="(.*?)", '$1.css')
LINK 項的屬性可以以任何順序出現。
如何修復搜索運算式,以便括號捕獲輸入中的 GUID?
uj5u.com熱心網友回復:
如果我理解正確,那么您正在尋找這個結果:
<link rel="stylesheet" type="text/css" href="c5fb04c9-44e6-40d1-bb1f-46b170a9e53f.css" id="style-standard" />
如果是這種情況,下面的正則運算式替換應該可以作業。有關詳細資訊,請參閱https://regex101.com/r/hSy1qm/1。
$a = '<link rel="stylesheet" type="text/css" href="https:/my.web.site/c5fb04c9-44e6-40d1-bb1f-46b170a9e53f" id="style-standard" />'
$a -replace '(?<=href=").*(?<guid>[\w\d]{8}-([\w\d]{4}-){3}[\w\d]{12}).*?(?=")', '${guid}.css'
值得注意的是,.Replace()字串方法與正則運算式不兼容,要使用正則運算式進行替換,您可以使用-replaceoperator。
如果您想首先測驗字串是否有GUID,您可以使用-match運算子和自動變數$Matches來設定條件:
$a = '<link rel="stylesheet" type="text/css" href="https:/my.web.site/c5fb04c9-44e6-40d1-bb1f-46b170a9e53f" id="style-standard" />'
$re = '(?<=href=").*(?<guid>[\w\d]{8}-([\w\d]{4}-){3}[\w\d]{12}).*?(?=")'
if($a -match $re) {
$a -replace $Matches[0], "$($Matches['guid']).css"
}
uj5u.com熱心網友回復:
使用 -replace 而不是 .replace():
$a = '<link rel="stylesheet" type="text/css" href="https:/my.web.site/c5fb04c9-44e6-40d1-bb1f-46b170a9e53f" id="style-standard" />'
$a -Replace 'href="(.*?)"', '$1.css'
<link rel="stylesheet" type="text/css" https:/my.web.site/c5fb04c9-44e6-40d1-bb1f-46b170a9e53f.css id="style-standard" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439100.html
標籤:电源外壳
