1

How to use Javascript to selectively replace the word called Rindan in he below text , but not if its in some attribute like img alt=="Rindan" I want to replace the word Rindans which is an inner text and not if it is in attribute .

`"<a href="http://edition.cnn.com/video/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-rindan-family-syria.cnn">Rindan's family on her legacy</a> &nbsp;<a href="http://edition.cnn.com/video/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-Rindan-family-syria.cnn" target=""><img alt="Rindan's family on her legacy" border="0" class="cnnVideoIcon" height="10" src="http://i.cdn.turner.com/cnn/.e/img/3.0/global/icons/video_icon.gif" width="16" /></a>`"
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • I need to use JS to parse a big HTML content and replace innerHtml's with some other data ..but if it is in attribute it should remain untouched .Further I will not be replacing with another name , but with a word . I am looking for an approach like htmlcontent = some string . Use htmlparser on this and replace only the textual contens . – Nishant Feb 28 '12 at 07:51
  • Input - HTML CONTENT as a text ------> Output replace spellcheck error words within a span class -----> ( Ofcourse I already have an array of words with erros ] . If the word is in some attribute just ignore it and replace only if its a text . – Nishant Feb 28 '12 at 07:58

4 Answers4

1

Give the id to a block, get the innerHTML, and use the replace string function:

var get=document.getElementById('test');
var str=get.innerHTML;
document.write(str.replace("Rindan's","TestNAme"));

set this back to innerHTML

see live demo on http://jsfiddle.net/kunalvashist/CeeDU/

Phil Norfleet
  • 85
  • 1
  • 12
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
  • tag is an example , I might have many tags .Consider the sources as a a big HTML document . I need to parse and replace only the inner HTML contents and not touch the other things . – Nishant Feb 28 '12 at 07:50
1

You can use the function parameter in replace function of string

your_string = your_string.replace(<regexp>,  
     function(matched, index){
        // check matched 
        return replacement;
     }); 
}); 

ref: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

Diode
  • 24,570
  • 8
  • 40
  • 51
0

this is to replace the text and not the attributes:

var aa = document.getElementsByTagName('a');
for(i=0;i<=aa.length;i++)
{
aa[i].innerText = aa[i].innerText.replace("Rindan","anyone");
}
​
Vikram
  • 8,235
  • 33
  • 47
  • DOM methods will not help me much I think . I need a combination of HTML parsing and regex . – Nishant Feb 28 '12 at 07:54
  • No, you don't. Regex is unnecessary, unless you've got more words, or instances of the same word perhaps, that you need to change. [Don't parse HTML with regex](http://stackoverflow.com/a/1732454/82548), it's not pretty. Or safe. – David Thomas Feb 28 '12 at 08:08
0

You could loop all children elements of the body tag and do a replace on the innerhtml.

var body_children = document.body.children;
for(var i = 0; i < body_children.length; i++){
   var str = body_children[i].innerHTML
   body_children[i].innerHTML = str.replace("this","with this");
}
lbragile
  • 7,549
  • 3
  • 27
  • 64
roel
  • 2,005
  • 3
  • 26
  • 41