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="/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-rindan-family-syrian">Rindan's family on her legacy</a> <a href="/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-Rindan-family-syrian" target=""><img alt="Rindan's family on her legacy" border="0" class="cnnVideoIcon" height="10" src="/.e/img/3.0/global/icons/video_icon.gif" width="16" /></a>`"
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./video/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-rindan-family-syria.cnn">Rindan's family on her legacy</a> <a href="http://edition.cnn./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./cnn/.e/img/3.0/global/icons/video_icon.gif" width="16" /></a>`"
Share
Improve this question
asked Feb 28, 2012 at 7:36
NishantNishant
22k20 gold badges78 silver badges106 bronze badges
2
- 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 <span class="spellcheck_error">word</span> . I am looking for an approach like htmlcontent = some string . Use htmlparser on this and replace only the textual contens . – Nishant Commented Feb 28, 2012 at 7: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 Commented Feb 28, 2012 at 7:58
4 Answers
Reset to default 1You 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/en/JavaScript/Reference/Global_Objects/String/replace
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/kunalvashist/CeeDU/
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");
}
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");
}