SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find text in document and then replace

    I need to find this line of text: "Catch Me If You Can"... and remove it.

    I was looking at something like this highlight script (http://www.tedpavlic.com/post_highli...javascript.php) but I'm not sure I can get that nice and small and doing what I need here.

    Any help?

    note: forum search isn't working for me, sorry if reposted.

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this doesn't seem to work: document.replace(/Catch Me If You Can/,""));

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah found the solution on a rather obscure page:

    HTML Code:
    <head>
    <script type="text/javascript">
    function MyHead() {
    var find = "boom";
    var repl = "";
    var page = document.body.innerHTML;
    while (page.indexOf(find) >= 0) {
    var i = page.indexOf(find);
    var j = find.length;
    page = page.substr(0,i) + repl + page.substr(i+j);
    document.body.innerHTML = page;
    }
    }
    </script>
    </head>
    <body onload="MyHead()">
    boom goes the bomb.
    </body>
    sorry to have posted =D

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hrm, this seems to break some of the elements on my page. Anyone have a better solution?

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To search through the text in a document while leaving the html alone, use dom methods. For example-

    Code:
    document.deepText= function (hoo,fun,arg){
    	var A= [],next,T,pa,i;
    	if (!hoo) return A;
    	if (hoo.nodeType== 3) A.push(fun(hoo, arg));
    	else if (hoo.hasChildNodes()) {
    		pa= hoo.childNodes, i= 0;
    		while (pa[i]) {
    			next= pa[i++];
    			T= next.nodeType;
    			if(T== 3) {
    				if (/\w+/.test(next.data)) A.push(fun(next,arg));
    			}
    			else if(T== 1) A= A.concat(arguments.callee(next,fun,arg));
    		}
    	}
    	return A;
    }
    Code:
    document.deepReplace= function(rfrom,rto){
    	document.deepText(document.body,function(hoo){
    		if(hoo.data)hoo.data= hoo.data.replace(rfrom,rto);
    	})
    }
    // You can use a string or a regular expression for the search argument.
    document.deepReplace('Catch me if you can','');
    document.deepReplace(/e/ig,'@');

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks good thanks. I'll let you know if I have any problems =P


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •