문제

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.

도움이 되었습니까?

해결책

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

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

다른 팁

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, "");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top