JQuery如何替换textarea中的字符串?

Har*_*rry 0 html javascript regex jquery replace

目前的Textarea:

<textarea id="event_content">
This text area could be full of information..www.london2012.com
And might contain upto 5 links that all need updating.
www.rio2016.org

Link already converted. this should be left.
<a href="http://www.thetimes.co.uk" target="_blank">www.thetimes.co.uk</a>
</textarea>
Run Code Online (Sandbox Code Playgroud)

在jquery之后所需的Textarea:所有链接都要用标签和目标attr进行清理/替换

<textarea id="event_content">
This text area could be full of information
<a href="http://www.london2012.com" target="_blank">www.london2012.com</a>
And might contain upto 5 links that all need updating.
<a href="http://www.rio2016.org" target="_blank">www.rio2016.org</a>

Link already converted. this should be left.
<a href="http://www.thetimes.co.uk" target="_blank">www.thetimes.co.uk</a>
</textarea>
Run Code Online (Sandbox Code Playgroud)

2012年7月21日 - Ωmega的代码表示感谢,但是可以通过保留已转换的链接来改进吗?

Ωme*_*ega 8

我相信你正在寻找这样的东西(点击这里测试这个小提琴):

$('#event_content').val(
  $('#event_content').val().replace(/\b(http(s|):\/\/|)(www\.\S+)/ig,
    "<a href='http\$2://\$3' target='_blank'>\$3</a>"));
Run Code Online (Sandbox Code Playgroud)