2012-10-04 13 views
1

I am using this regex to try to find links that look like this and replace them with only the content between the start and end tag of the link.Javascript 정규식 대신 <a href tag

The regex:

/<a class="tag_link"[^>]*>([^<]*)<\/a>/mg 

I tested the regex on a sample code and it works, but when i try to apply it on object.innerHTML it doesnt replace anything. What is wrong?

The code:

document.getElementById(preview_div_ID).innerHTML.replace(/<a class="tag_link"[^>]*>([^<]*)<\/a>/mg, '$1'); 
document.write(document.getElementById(preview_div_ID).innerHTML); 

Some sample html that is in "document.getElementById(preview_div_ID).innerHTML" above:

<h5 style="line-height: 9px; margin: 2px; "> 
      <span style="font-size: 7px; line-height: 8px; "> 
       Ost- (caseous) <a class="tag_link" href="javascript:;" onclick="open_tag('nekros', this);">nekros</a> 
      </span> 
     </h5> 

Any help is much appreciated

+11

***WHY*** are you using regex when you've got a DOM to use?! –

+2

^that, a million times that! –

+2

Don't regex html. Written in stone somewhere which is probably why you didn't see it online! So now you know... –

답변

2

instead of

document.getElementById(preview_div_ID).innerHTML.replace(/<a class="tag_link"[^>]*>([^<]*)<\/a>/mg, '$1') 
document.write(document.getElementById(preview_div_ID).innerHTML); 

It should be

document.getElementById(preview_div_ID).innerHTML = document.getElementById(preview_div_ID).innerHTML.replace(/<a class="tag_link"[^>]*>([^<]*)<\/a>/mg, '$1') 
+2

It shouldn't be anything involving regexes. – saml

+0

+1 Answers the OP's question - should add a note that this is not the preferred method for parsing HTML in javascript. – JDB

+0

'document.write(document.getElementById(preview_div_ID).innerHTML);"' is used only to check the result of my replace for debugging purpouses. –

0

This could be solved by letting JS create DOM elements from your HTML. I will be using jQuery for simplicity in my answer.

var $code = $('<h5 style="line-height: 9px; margin: 2px; "> 
      <span style="font-size: 7px; line-height: 8px; "> 
       Ost- (caseous) <a class="tag_link" href="javascript:;" onclick="open_tag(\'nekros\', this);">nekros</a> 
      </span> 
     </h5>'); 

$code.find('a').replaceWith(function() { 
    return $(this).text(); 
}); 

Please note that this code isn't tested

+0

I dont use jquery, and i am afraid that I dont understand how I would use your solution without it. Is there a "replacewith" function built in to javascript? –

+0

@ErikBoberg See [this question/answer](http://stackoverflow.com/questions/843680/how-to-replace-dom-element-in-place-using-javascript) on how to replace DOM elements without jQuery – Znarkus

5

Try this instead:

var links = document.getElementsByTagName("a"); 
for(var link in links){ 
if(links[link].getAttribute("class").indexOf("tag_link") != -1){ 
    var oldLink = links[link]; 
    var newLink = document.createElement("a"); 
    var newLink.innerHTML = oldLink.innerHTML; 
    oldLink.parentNode.insertBefore(newLink , oldLink); 
    oldLink.parentNode.removeChild(oldLink); 
} 
} 

It will iterate through all links that have the class name "tag_link" and then remove all of their attributes while retaining the link text. You can change this to just a single element if you by id, just make the foreach loop target the single dom element. Like this:

function stripAnchor(anchorId){ 
    var oldLink = document.getElementById(anchorId); 
    var newLink = document.createElement("a"); 
    var newLink.innerHTML = oldLink.innerHTML; 
    oldLink.parentNode.insertBefore(newLink , oldLink); 
    oldLink.parentNode.removeChild(oldLink); 
} 
+0

Thank you Travis, I implemented your solution and it works. It still leaves empty blabla 태그를 찾아야하지만, 실제로는 문제가되지 않습니다. 대신 원하는 경우 해당 태그를 으로 쉽게 변경할 수 있습니다. 당신은 이것이 정규식을하는 것보다 빠르다는 확신을 갖고 있습니까? HTML 코드에 100 개의 링크가 있고 그 중 5 개가 class = "tag_link"라고 가정 해 보겠습니다. 귀하의 방법을 사용하여 나는 모든 100 개의 링크를 통해 반복하고 대신 정규식을 실행하고 내가 영향을주고 싶은 5를 교체 그들을 확인해야합니다. 나는 꽤 javascript에 익숙하지 않고 어떤 방법이 빠르고 느린 지 알지 못한다 ... –

+0

@ErikBoberg - var links = document.getElementsByTagName ("a");'는 빠르게 실행되고 O (n)을 두려워하지 않는다. 그럼 당신은 그 세트를 반복하고 있습니다. –