나는 거의 효과가있다. 이 많이 있는지 알고 싶습니다. 더 좋은 방법입니다.일반 URL을 링크로 바꾸는 방법 (예 :
function replaceURLWithHTMLLinks(text) {
text = text.replace(/a/g, "--ucsps--");
text = text.replace(/b/g, "--uspds--");
var arrRegex = [
/(\([^)]*\b)((?:https?|ftp|file):\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|])(\))/ig,
/(\([^)]*\b)((?:https?|ftp|file):\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|])(.?\b)/ig,
/()(\b(?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_()|!:,.;]*[-a-z0-9+&@#\/%=~_()|])(.?\b)/ig];
for (i = 0; i < arrRegex.length; i++) {
text = text.replace(arrRegex[i], "$1a$2b$3");
}
text = text.replace(/a([^b]*)b/g, "<a href='$1'>$1</a>");
text = text.replace(/--ucsps--/g, "a");
text = text.replace(/--uspds--/g, "b");
return text;
}
var elm = document.getElementById('trythis');
elm.innerHTML = replaceURLWithHTMLLinks(elm.innerHTML);
어떤 생각?
생각으로 대체 : messy_ 보이는 _wow. 다른 질문 있니? – Halcyon
이것은 더 적당 할 것입니다 http://codereview.stackexchange.com/ – ediblecode
대체하는'innerHTML'는 위험 할 것입니다. 트리를 탐색하고 각 요소의 'innerText'만 바꾸십시오. – Mohsen