1

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?

Prophet
  • 32,350
  • 22
  • 54
  • 79
jartaud
  • 375
  • 5
  • 19
  • This question makes no sense... – wilsonpage Jan 26 '11 at 23:10
  • 1
    @pagewil He's asking for a regular expression to strip out anchor tags along with the anchor text within the tags. @jartuad what you need is a regular expression. You can probably find an example with a quick google search. – SRM Jan 26 '11 at 23:16
  • @pagewil thanks anyway and I'm sorry for my english, but STO is not WWE (C). – jartaud Jan 27 '11 at 13:42

2 Answers2

3
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.

Community
  • 1
  • 1
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • Thanks, it works great. Sorry for the delay, I had a serious problem with my Internet connection. – jartaud Jan 27 '11 at 13:31
  • @jartaud: There was a slight mistake in the above regex which caused `text here` in `hellotext hereworld` to be deleted. I changed `[\s\S]*` to `[\s\S]*?`, which fixes it. – PleaseStand Jan 27 '11 at 21:34
1

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

Mauricio
  • 5,854
  • 2
  • 28
  • 34
  • 1
    Almost, but not quite. That sanitizer will only remove html markup. What he wants to remove is the markup *and* the text within the anchor tags. – SRM Jan 26 '11 at 23:21