Question

In a textarea people can paste text. This can bring a lot of unwanted attributes like the style attribute. I am trying to create a regex that removes all of these attributes, but leaves the html tags as they are.

I cannot seem to get it to work. I have tried several RegExps from searches, but they don't seem to fit. ALL of the children and ALL of the elements in the textarea should be stripped down by ALL of its attributes.

How should this RegExp look like??

My current RegExp (found on regex for removing attributes):

text.replace(new RegExp(/<\s*(\w+).*?>/, 'igm'), '');

Please help me get to the right regex

EDIT

To clarify, this:

<strong style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 13px; line-height: 13.008000373840332px;"><u style="box-sizing: border-box;">Trade</u></strong>

should be replaced with:

<strong><ul>Trade</ul></strong>

IMPORTANT: this can occure multiple times in the textarea, so all should be replaced.

EDIT

Also, is it smart to replace all <strong> tags with <b>? This way the text is even shorter.

Était-ce utile?

La solution

This is dangerous territory parsing HTML with regex, but what about.

text.replace(/<(\w*) .*?(\/?)>/g,"<$1$2>")

Autres conseils

This looks like what you might need:

text.replace(/(<\s*\w+)(.*?)>/, '$1>')

This removes the attributes from one opening tag.

This one worked:

str.replace(/<[^>]*?(\s+[^=]+="[^"]*?")>/g, "");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top