Search code examples
htmlangularjssummernote

How to remove <p><br></p> tags in summernote value?


While entering space in staring am getting following code from summernote textarea,

<p><br></p><p><br></p><p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p><p><br></p><p><br></p>

but i want to remove the starting <p><br></p><p><br></p> before storing to db.

for example i want to store like below,

<p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p>

Solution

  • If you are sure about the pattren than You can achieve it something like

    var data='<p><br></p><p><br></p><p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p><p><br></p><p><br></p>';
    while(data.startsWith('<p><br></p>')){
    data=data.replace('<p><br></p>','')
    }
    
    while(data.endsWith('<p><br></p>')){
    data=data.replace(new RegExp('<p><br></p>$'),'')
    }
    console.log(data)