I want to remove the below tag from an HTML object containing the sitecore field value in the form of HTML.

I want to remove this and set back the remaining HTML in the same field.

<div class = "abc"> Some other tags and HTMLs </div>

I referred the below link but no luck.

Using Powershell Extensions to remove empty <p> tags from all rich text fields

Please also someone tell me that what will be the Regex pattern for <div class = "abc"> for sitecore powershell.

  • Is the html markup from the rich text field? – Hishaam Namooya Jun 25 '20 at 10:40
  • @HishaamNamooya Yes – Divyansh Gupta Jun 25 '20 at 11:01
  • You want to remove the first div only or the div including the other tags within that div? – Hishaam Namooya Jun 25 '20 at 11:04
  • div including the other tags within that div – Divyansh Gupta Jun 25 '20 at 11:12
  • Really need something like HtmlAgilityPack to parse the whole partial doc. This is because you need to relocate the child content of the div. Using a regex to find and replace will be risky. – Michael West Jun 25 '20 at 22:24

You can use the following snippet to remove the HTML tags.

$item = Get-Item -Path "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"
$richTextField = $item["Text"]
$richTextHtml = $richTextField -replace '<.+?>' 
Write-Host $richTextField
Write-Host "New Value"
Write-Host $richTextHtml

I tested it on the home item. Below is the result in the console. enter image description here

  • Thanks for your valuable answer, I believe this will remove every div tag within the HTML, in my case I need to remove only those divs, which are having some particular class, For e.g. <div class ="a" I want to remove but I don't want <div class="b" to be removed within the HTML. – Divyansh Gupta Jun 25 '20 at 11:47
  • I will see if this is possible as we would need to find the closing div tag also to make sure the HTML is valid. – adarsh Jun 25 '20 at 14:09
  • Can anyone suggest on the solution here. [string]$content = $GetContent $content = $content.Replace('<div class="parsys"><div class="heading section"><h1 class="news heading01">@Html.Sitecore().Placeholder("NewsTitle")</h1></div>', "") This is not working for me. – Divyansh Gupta Jul 2 '20 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.