2017-01-12 1 views
-1

문자열의 앵커 태그를 링크로만 텍스트로 바꾸려면 어떻게해야합니까?jQuery 앵커 태그를 텍스트 영역의 href url로 바꿉니다.

실제 :

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 

필요가 또 다른 링크를 교체하기 위해 HTTP : //local.mysite.com/test/f/match

답변

0

가정 당신은 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 사이의 공간에 대한주의하십시오. 항상 정확한 HTML로 인식되지 않는 < a이 있습니다. 너의 < /a에 같은.

+0

감사합니다. 작동했습니다. – user3565199

+0

NP. 나는 사람들이 복사/붙여 넣기를 텍스트 영역에 넣은 곳과 비슷한 것을 최근에했고 붙여 넣은 데이터에 html 코드가있는 것을 처리해야했습니다. 이 트릭은 매우 편리했습니다. – nurdyguy

관련 문제