0

I have requirement where I have to Identify 2 point.

  1. Replace all tag that is having closed tag but exculding few tags Like <a></a>, <p></p>, <i></i>. For this I did lots of R&D but not able to find exact solution. My final finding regx : (?!<p|a|i|>)(?<opentag><(?!/)[^>]*[^/]>)

But this is also finding tag that not having closing tag Like <abc>

NOTE: In this I want to replace start and close tag with space.

  1. If we have Tag that not having closing tag then I want to remove only special symbol not whole tag. <abc> should be abc

Example:

Input String:

   <br />
   <ul id="matchMe" type="square">
      <li>stuff...</li>
      <li>more stuff</li>
      <li>
          <div>
               <span>still more</span>
               <ul>
                    <li>Another &gt;ul&lt;, oh my!</li>
                    <li>...</li>
               </ul>
          </div>
      </li>
   </ul>
<p>....</p>
<p class="hhh">....</p>

<customTag1> Hello <CustomTag2>

Output String:

      stuff...
      more stuff


               still more

                    Another &gt;ul&lt;, oh my!
                    ...




<p>....</p>
<p class="hhh">....</p>

customTag1 Hello CustomTag2

In this example I do not want to do anything with p tag but all other tag that having self closing tag or closing tag should be replace with blank. customTag1 and CustomTag2 both tags are custom and it's looking like HTML start tag but do not having end tag. For these tag I just want to remove <,> symbol only.

Few answer helped here but not fully https://stackoverflow.com/a/7564061/4813631 https://stackoverflow.com/a/10267932/4813631

Thanks

A.K.
  • 2,284
  • 3
  • 26
  • 35
  • Could you give us an example of the initial string and the desired output to be more clear your question? I mean, something like "this is my html and that is my desired output" – Pablo May 14 '19 at 11:26
  • 1
    @Pablo I added example. I hope example will enough to understand my issue. – A.K. May 14 '19 at 12:03

1 Answers1

0

It should works:

let html = `<br />
<ul id="matchMe" type="square">
   <li>stuff...</li>
   <li>more stuff</li>
   <li>
       <div>
            <span>still more</span>
            <ul>
                 <li>Another &gt;ul&lt;, oh my!</li>
                 <li>...</li>
            </ul>
       </div>
   </li>
</ul>
<p>....</p>
<p class="hhh">....</p>

<customTag1> Hello <CustomTag2>
`
let allInitialsTags = html.match(/<([a-zA-Z\d]*(?=\s)*(?!\/))/g)
allInitialsTags = allInitialsTags.map(el=>el.replace("<",''))
let allEndTags = html.match(/(<\/[a-zA-Z\d]*\s*)/g)
allEndTags = allEndTags.map(el=>el.replace("</",''))
const tagWithNotClosingTag = []
allInitialsTags.forEach(el=>{
     if(!allEndTags.includes(el)){
          tagWithNotClosingTag.push(el)
     }
})
tagWithNotClosingTag.forEach(el=> {
     html = html.replace(RegExp(`<${el}>`,'g'),`${el}`)
})

const result = html.replace(/<[^(p|a|i)]?[^(\/p|\/a)?][^>]*>/g,"")
console.log(result)
Pablo
  • 2,137
  • 1
  • 17
  • 16