1
<html>
<head>
<title>Test</title>

</head>
<body>
<a href="hello.html">hello</a>
<script>
var str=document.body.innerHTML;
document.body.innerHTML=str.replace(/hello/g, "hi");</script>
</body>

</html>

In this code hello.html and hello will change hi.html and hi. I don't want to replace href="". How to write regular expression for that ?

saturngod
  • 24,649
  • 17
  • 62
  • 87
  • You *could* use jQuery `$('*')` and then replace each element's `html()`. But it would be rather expensive and not straight-forward. – pimvdb Mar 01 '11 at 10:47

2 Answers2

1

The following regex replace wil do what you want:

<script>
var str=document.body.innerHTML;
document.body.innerHTML=str.replace(/(>[^<]*)hello/g, "\1hi");
</script>

But I think it is still fragile, and any solution with regex replaces in .innerHTML will be... Remember that regexes are always a hacky solution when trying to solve problems which involve html/xml parsing.

markijbema
  • 3,985
  • 20
  • 32
0

What do you need this for? Am I guessing correctly when I say that you want to replace all the text content of the document?

In that case, I would suggest getting a list of all content nodes from the DOM (see this question’s accepted answer for two ways to do this, one with jQuery and one without).

Using that, you could then apply your function to update each text node's contents:

var textNodes = getTextNodesIn(el);
for (var i = 0; i < textNodes.length; i += 1) {
    textNodes[i].innerHTML = textNodes[i].innerHTML.replace(/hello/g, "hi");
}

This would leave all the HTML attributes unaffected. If you want to adjust those as well (excepting, of course, any href attribute), you could expand the getTextNodes function to include attributes (excepting href attributes) in the returned list of nodes.

Community
  • 1
  • 1
Martijn
  • 13,225
  • 3
  • 48
  • 58