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 ?

1
  • You could use jQuery $('*') and then replace each element's html(). But it would be rather expensive and not straight-forward. Commented Mar 1, 2011 at 10:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.