jQuery 将文本区域中的锚标记替换为 href url
jQuery replace anchor tag with href url in textarea
如何将字符串中的锚标记替换为仅包含 link 的文本。
实际:
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>"
期望的结果:
This is a test article with links which need to be replaced. http: //local.mysite.com/test/f/english-as-a-second/p/addpost
需要替换的另一个 link 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);
特别提示:注意<
和a
之间的space。您的 < a
并不总是被认为是正确的 html。你的 < /a
.
也一样
如何将字符串中的锚标记替换为仅包含 link 的文本。
实际:
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>"
期望的结果:
This is a test article with links which need to be replaced. http: //local.mysite.com/test/f/english-as-a-second/p/addpost
需要替换的另一个 link 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);
特别提示:注意<
和a
之间的space。您的 < a
并不总是被认为是正确的 html。你的 < /a
.