jQuery如何替换文本区域内的字符串? [英] JQuery How to replace strings inside a textarea?

查看:71
本文介绍了jQuery如何替换文本区域内的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前文本区域:

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


jquery之后需要的Textarea:所有要用标签和目标属性清除/替换的链接


Desired Textarea after jquery: all links to be cleaned/replaced with tags and target 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>


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


21 July 2012 - Ωmega's code worked a treat thanks, but could be improved by leaving already converted links?

推荐答案

我相信您正在寻找类似的东西(单击

I believe you are looking for something like this (click here to test this fiddle):

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

这篇关于jQuery如何替换文本区域内的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆