-2

I have this regular expression for delete inline styles:

(style=").*"

I have 2 problems. Example:

<p style="margin:0pt; line-height:115%; font-size:11pt">

In this case all is fine, but I need select the space before P

Problem 2, If there are, more html tags in the same line, the expression doesn't work, and removes the "TEXT VERY IMPORTANT". Example:

<p style="margin:0pt; line-height:115%; font-size:11pt">TEXT VERY IMPORTANT 1 <p style="margin:0pt; line-height:115%; font-size:11pt">TEXT VERY IMPORTANT 2 <p style="margin:0pt; line-height:115%; font-size:11pt">TEXT VERY IMPORTANT 3

I need select only SPACE+style="..." (for then remove it) Any help? Thank you.

FeKuLa
  • 356
  • 2
  • 10
  • 24

2 Answers2

0

HTML is structured data that regex doesn't understand, which means you run into exactly the sort of issues you're having: for any non-trivial problem, the many allowed variations in the format make it very difficult to parse using string manipulation techniques.

DOM methods are designed for manipulating that sort of data, so use them instead. The following code should be self-explanatory:

document.querySelectorAll('*').forEach(function(el) { 
    el.removeAttribute("style")
});
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
-2

This regular expression for remove all attributes, solves my problem. (I have only 1 attribute).

((?<=<p))[^>]*(?=>)
FeKuLa
  • 356
  • 2
  • 10
  • 24