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
***WHY*** are you using regex when you've got a DOM to use?! –
^that, a million times that! –
Don't regex html. Written in stone somewhere which is probably why you didn't see it online! So now you know... –