1

I'm trying again to replace each text content of each control in a page using javascript and jquery.

I need to search in each text content (WITHOUT MODIFING TAGS, ONLY THE TEXT) any word and replace it with another any word.

One try is:

jQuery.fn.replaceEachOne = function (objective, rep) {
   this.each( function(){ 
                //$(this).html( $(this).html().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
    $(this).html( $(this).html().replace( new RegExp('('+objective+'(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}

Please help!!

3 Answers 3

1

Something like this should do the trick:

$.fn.replaceEachOne = function(search, replace) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(search, replace);
        } else if (this.nodeType == Node.TEXT_NODE) {
            this.nodeValue = this.nodeValue.replace(search, replace);
        }
    });
};

This does the replacement on text nodes directly, rather than modifying the HTML of an entire element. Note that it is case-sensitive. You'd need to change the call to replace to use a regular expression if you want a case-insensitive search.

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

2 Comments

Thats work, but I have to add a new control to the finded text, I mean add: <label Color="..."> Finded Text </label> and if I do it in that way the browser dont recognize it as control, only as text, I mean, it apears textually <label Color="..."> Finded Text </label> not a Cool label with a diferent color... :( help!!
@Daniel G.R. Sounds like you need a highlighting plugin.
0

I would start by something like this:

jQuery.fn.replaceEachOne = function(objective, rep) {
    var html = jQuery(this).html();
    var simpleRegexp = new RegExp(objective, "gi");
    var regexp = new RegExp(">[^><]*?"+objective+"[^><]*?<","gi");
    html = html.replace(regexp, function(match) {
        return match.replace(simpleRegexp, rep);
    });
    jQuery(this).html(html);
}

That code finds the matching text in body html code beetween '>' and '<' characters and then replaces matched text.

Of course this is simple solution and it will replace text also in <script> or <style> blocks. By I think it is a good idea to start with.

4 Comments

But the problem is that I dont want to add content into the <> I need to add content in <> HERE </>
I've wrote "beetween '>' and '<'", not "beetween '<' and '>'". The order is important.
Edit info: I've replaced "body" by this.
I've tried that: jQuery("body").replaceEachOne("jquery", "<label style='color:red'>jquery</label>"); on jQuery.org and it chenged color of all jquery occurences on that page.
0

Finally...

jQuery.fn.replaceEachOne = function (objective, reposition) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(objective, reposition);
        } else if (this.nodeType == Node.TEXT_NODE) {
            var label = document.createElement("label");
            label.innerHTML = this.nodeValue.replace(objective, reposition);
            this.parentNode.insertBefore(label, this);
            this.parentNode.removeChild(this);
        }
    }); 
}

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.