I'm looking to do a search & replace on one of my sites so I can implement some kind of glossary system.
Here's the basic code I've got so far for a quick test page...
<html>
<head>
<script language="javascript">
function replace_stuff() {
document.body.innerHTML = document.body.innerHTML.replace(/test/g,'sample');
}
</script>
</head>
<body onLoad="javascript:replace_stuff();">
Line 1 : test<br />
Line 2 : <a href="http://www.test.com/">http://www.test.com/</a>
</body>
</html>
But, there's a problem with this.
Line 1 works perfectly. It replaces "test" with "sample", and all is well with the world.
The problem is with Line 2. If the text is inside a link, either as the actual URL or as part of the anchor text, I want it to completely skip over it and not replace it. Line 2 should remain untouched.
Anybody got any ideas on how to do this one?
Btw, the query has changed slightly...
document.body.innerHTML = document.body.innerHTML.replace(/\b(test)\b/g, 'sample');
So, now something like "testicles" (sorry, couldn't think of another word), doesn't get turned into sampleicles, however because of the .'s in http://www.test.com
, that still gets changed to http://www.sample.com
.
add a comment