If the backend returns the content in the rich text editor to the front end, you can use this method to retain only the text.
There are several formats for labels
1.<div class="test"></div>
2.<img />
3. Custom tags<My-Tag></My-Tag>
For the above tags, the regular rules determined are reg=/<\/?.+?\/?>/g
<Denote angle brackets
The first \/? indicates the situation of this tag </div>
.+? means that everything in the middle is replaced
The second \/? means <img/>
/g means global replacement
The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Remove all tags</title> </head> <script> function matchReg(str){ let reg=/<\/?.+?\/?>/g; ((reg,'')); } matchReg(`<p>Remove allhtmlLabel,<img/><My-Tag class="abc" value="test">自定义Label也可以去除哦</My-Tag></p>`); </script> <body> </body> </html>
Summarize
The above is what the editor introduced to you using regular expressions to remove all html tags and only retain text. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!