0

I want to replace all "i" character in <body> with "I" by JavaScript. (for example: <div id="123">Hi! it!</div> should change to <div id="123">HI! It!</div>). I know I must use regular expressions. but I don't know what I should write. Can you help me? Thanks ..

6
  • 1
    Why on earth do you want to do that? Commented Dec 20, 2010 at 13:06
  • 6
    How do you know that you 'must' use regex? Commented Dec 20, 2010 at 13:06
  • Because I want to replace "i" with "I", only in contents and not in tags ... Commented Dec 20, 2010 at 13:13
  • 3
    If it's HTML/XML, don't use regular expressions. Historic archives of Stackoverflow will tell you why. Commented Dec 20, 2010 at 13:15
  • @darioo: I begin to see you guys' point (and weariness). Commented Dec 20, 2010 at 13:17

2 Answers 2

3

I just answered a similar question which may help you: JQuery/Javascript - Search DOM for text and insert HTML

For this particular case, you can simplify.

UPDATE

I've added a filter parameter to allow you to filter out descendants of particular nodes. This should be a function that takes a single DOM node parameter and returns a Boolean: true to replace text within the node and its descendants and false to ignore the node and its descendants.

function replaceText(node, regex, replacementText, filter) {
    if (node.nodeType == 3) {
        node.data = node.data.replace(regex, replacementText);
    } else if (!filter || filter(node)) {
        var child = node.firstChild;
        while (child) {
            replaceText(child, regex, replacementText, filter);
            child = child.nextSibling;
        }
    }
}

function scriptAndStyleFilter(node) {
    return node.nodeType != 1 || !/SCRIPT|STYLE/i.test(node.nodeName);
}

replaceText(document.body, /i/g, "I", scriptAndStyleFilter);
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks ... your function is great! but how can filter <script>, <style> tag? this means stop replacing in these tags content.
@kikio: What exactly is the problem with it? By the way, <style> elements are not valid within the <body> element of an HTML document (even though it does work in most browsers).
I wrote on that page: <style type="...">#myid {font-family: tahoma}</style><span id="myid">Hi! it!</span> I want it changed to this: <style type="...">#myid {font-family: tahoma}</style><span id="myid">HI! It!</span> (all of <style> contents must don't be changed)
Right... and that's what happens.
(with your new code): <style type="...">#myId {font-famIly: tahoma}</style><span id="myid">HI! It!</span>
|
1

Here's an alternative, using CSS, jQuery and a Highlight plugin:

CSS:

.highlight {text-transform:uppercase;}

JavaScript:

$('body').highlight('i');

Working example: http://jsbin.com/okavo4

This is a quick example, but you can use the source code to get what you want, in case you need something more complex. Using a regular expression on the source of your body is wrong, specially in context of a browser, where you already have a parsed DOM structure.

1 Comment

@kikio - I did just that, and in just a few minutes. The plugin allows you to exclude certain elements, if that is what you need. A regex here would be difficult or down right impossible.

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.