0
<p style="text-align: right;">
   <p style="text-align: center;">
      <p style="text-align: left;">
         <p style="text-align:center;">
            leave this content
         </p>
      </p>
   </p>
</p>
<br>

save the content and the last paragraph with the style that has been applied, which is

<p style="text-align: right;">

7
  • expected output please you write on question. Nov 13, 2014 at 9:35
  • it is outside, and it can be more than two, three
    – topC
    Nov 13, 2014 at 9:35
  • expected output: <p style="text-align: right;">leave this content</p><br>
    – topC
    Nov 13, 2014 at 9:36
  • 1
    Use an HTML parser not a regex. A regex is the wrong tool: 1. they are not good – without extensions that JS's regex engine does not have – at matching grouping structures, 2. there are consequences.
    – Richard
    Nov 13, 2014 at 9:38
  • It is unfortunate that nested paragraphs are invalid HTML else jQuery would be able to do this easily :( Nov 13, 2014 at 9:41

3 Answers 3

0

For the concrete case that you provided try this simple code:

var elementToKeep = $('selectorToParagraph');

elementToKeep.html(elementToKeep.text());

For more universal cases you'll have to use an HTML parser.

2
  • ok, and how should I keep that content wrapped with last paragraph
    – topC
    Nov 13, 2014 at 9:52
  • the problem is that rich editor reads wrong the styles of nested paragraph. Especially if it is wrapped with other html tags
    – topC
    Nov 13, 2014 at 9:53
0

Regex is

<p style="(.*?)">(.*?)<\/p>

then try this

content = content.replace("<p style="(.*?)">(.*?)<\/p>","<p style="$1">$2</p>");
1
  • it leaves me with closing p tags i.e </p></p></p>
    – topC
    Nov 13, 2014 at 10:13
0

I try this

<p(?: [^>]+)?>((?:(?!<\/?p[ >]).)*)<\/p>

change the style as right for para. This only possible in regex.

SEE DEMO: http://regex101.com/r/dJ5nB1/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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