开发者

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

开发者 https://www.devze.com 2023-02-07 10:43 出处:网络
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 re

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消