2012-10-04 85 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)。那麼你只是迭代該集合。 –

相關問題