Regex PowerShell正则表达式以图像文件名替换alt

Regex PowerShell正则表达式以图像文件名替换alt,regex,powershell,Regex,Powershell,到目前为止,我有以下代码: 该正则表达式当前会查找任何没有alt标记的图像 我想更进一步,添加一个alt属性,其中的内容是src属性中图像的名称 和往常一样,我们非常感谢所有的建议或帮助 如果您想了解Automation功能的功能,请点击这里: function automate($school, $query, $replace) { $processFiles = Get-ChildItem -Exclude *.bak -Include "*.html", "*.HTML", "

到目前为止,我有以下代码:

该正则表达式当前会查找任何没有alt标记的图像

我想更进一步,添加一个alt属性,其中的内容是src属性中图像的名称

和往常一样,我们非常感谢所有的建议或帮助

如果您想了解Automation功能的功能,请点击这里:

function automate($school, $query, $replace) {
    $processFiles = Get-ChildItem -Exclude *.bak -Include "*.html", "*.HTML", "*.htm", "*.HTM" -Recurse -Path $school
    foreach ($file in  $processFiles) {
        #$text = Get-Content $file
        $text = Get-Content $file | Out-String
        $text = $text -replace $query, $replace
        $text | Out-File $file -Force -Encoding utf8
    }
}

试着这样做:

Get-ChildItem -Exclude *.bak -Include *.html, *.htm -Recurse -Path $school | % {
  $html = New-Object -COM HTMLFile
  $html.Write([IO.File]::ReadAllText($_.FullName))
  $html.getElementsByTagName('img') | % {
    $_.alt = Split-Path -Leaf $_.src
  }
  [IO.File]::WriteAllText($html.documentElement.outerHTML)
}
你应该。