0 개 이상의 URL을 포함 할 수있는 문자열이 있습니다. 문자열의 각 URL을 <a></a>
에 싸여있는 해당 URL로 바꿔야합니다. replace()
안에 현재 일치하는 개체에 대한 참조를 가져올 수있는 방법이 있습니까? 예를 들어정규 표현식에 대한 자바 참조
:
var msg = "Go to http://google.com and youtube.com";
var regex = /[[email protected]:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[[email protected]:%_\+.~#?&//=]*)?/gi;
msg.replace(regex, "<a href='"+<blank>+"'></a>") // Where <blank> is a reference to the current matched url
감사합니다!
http://stackoverflow.com/questions/37684/replace-url -with-html-links-javascript/2166104 # 2166104 당신의 대답이 있습니다. – glomad
위대한 작품! 하지만 난 어떻게 xss를 방지합니까? 난 단지 URL을 링크하고 싶지만, jquery의 .text()와는 작동하지 않을 것이고, .html()은 모든 HTML을 구현할 것입니다. –