jQuery在textarea中将锚定标记替换为href url

jQuery在textarea中将锚定标记替换为href url,jquery,anchor,Jquery,Anchor,如何将字符串中的锚定标记替换为仅作为文本的链接 实际: var text = "This is a test article with links which need to be replaced. < a href=" http://local.mysite.com/test/f/english-as-a-second/p/addpost">local.mysite.com/.../addpost< /a> Another link which needs to be

如何将字符串中的锚定标记替换为仅作为文本的链接

实际:

var text = "This is a test article with links which need to be replaced. < a href=" http://local.mysite.com/test/f/english-as-a-second/p/addpost">local.mysite.com/.../addpost< /a> Another link which needs to be replaced < a href=" http://local.mysite.com/test/f/match">local.mysite.com/.../match < /a>"

另一个需要替换的链接http://local.mysite.com/test/f/match

假设允许您使用jQuery,这个链接就可以工作

var str = 'This is a test article with links which need to be replaced. <a href=" http://local.mysite.com/test/f/english-as-a-second/p/addpost">local.mysite.com/.../addpost</a> Another link which needs to be replaced <a href=" http://local.mysite.com/test/f/match">local.mysite.com/.../match </a>';
var $str = $('<div/>').html(str);
$str.find('a').each(function ()
{
    $(this).replaceWith($(this).attr('href'));
});
var result = $str.text();
console.log(result);
var str='这是一篇带有需要替换的链接的测试文章。另一个需要更换的链接';
var$str=$('').html(str);
$str.find('a')。每个(函数()
{
$(this.replace为($(this.attr('href'));
});
var result=$str.text();
控制台日志(结果);

特别注意:注意
NP之间的空格。我最近做了类似的事情,让人们复制/粘贴到文本区域,我需要处理他们在粘贴的数据中使用html代码的情况。这个把戏很有用。
var str = 'This is a test article with links which need to be replaced. <a href=" http://local.mysite.com/test/f/english-as-a-second/p/addpost">local.mysite.com/.../addpost</a> Another link which needs to be replaced <a href=" http://local.mysite.com/test/f/match">local.mysite.com/.../match </a>';
var $str = $('<div/>').html(str);
$str.find('a').each(function ()
{
    $(this).replaceWith($(this).attr('href'));
});
var result = $str.text();
console.log(result);