简体   繁体   中英

How can I remove all anchor + anchor text from a Textaea?

I'm getting something like this in a textarea:

`Some text <a href="">Click me!</a>`

how can I remove all chars included Click me! from < to > using javascript replace method or using something similar?

var ta = document.getElementById('id-of-textarea');
ta.value = ta.value.replace(/<a(|\s[^>]*)>[\s\S]*?<\/a>/gi, '');

will cover the most likely cases. You probably shouldn't generalize this to a more complex situation though.

This does not replace the need to sanitize your input on the server side. In fact, the above should probably be done on the server side if at all possible, and a JavaScript approach (probably ignored by the spam bots anyways) used only if, say, this is off-the-shelf blogging software and you cannot modify it.

you can check this google caja plugin to sanitize the input. BUT! you MUST sanitize your input on the server side too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM