我有一个大字符串。
我想从大字符串中找到特定标记并替换该标记内的内容。此标记可能会多次出现,每次都必须更换。
以下是我的代码:
$FirstString = '<tag>'
$lastString = '</tag>'
$contentVal = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <tag> Aha Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes</tag>, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, <tag> Aha imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.</tag> Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. <tag> Aha Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum.</tag>"
if ($contentVal.Contains('<tag>')) {
$Pos1 = $contentVal.IndexOf($FirstString) + $FirstString.Length;
$Pos2 = $contentVal.IndexOf($lastString);
#Find text between <tag> and </tag>
$FoundString = $contentVal.Substring($Pos1, $Pos2 - $Pos1);
Write-Host $FoundString
#Modify this FoundString by finding Aha and replacing with <br/>
$FinalString = $FinalString.Replace("Aha","</br>")
Write-Host $FinalString
}
现在,我想将此$finalString
附加到$contentVal
与之前相同的位置。
此外,我有多次出现<tag>
。
如何操作每个出现并附加到主字符串?