/    Sign up×
Community /Pin to ProfileBookmark

Changing a specific string

I’m curious if there is a possibility of being able to search the whole document for a specific string using replace(“that”, “this”) for exaple to find any refference to “this” and replace it with “that” and then show the modified content.

Hopefully that will make sense to someone ?

Thanks Malb ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 31.2010 — <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
function findText(snippet) {
var newSourceCode, count=0;
newSourceCode= document.body.innerHTML;
re = /[^<][ws]*leo/gi;
newSourceCode= newSourceCode.replace(re,'FOO BAR');
document.body.innerHTML = newSourceCode;
}
</script>

<style type="text/css">
.highlight {
background:#ff0;
color#00f;
}
</style>

</head>
<body>
<form action="" method="" name="form1">
<div>
<input type="text" id="snippet">
<button type="button" onclick="findText(document.getElementById('snippet').value)">Replace</button>
</div>
</form>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi leo. Ut ac est nec odio ornare venenatis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi leo. Ut ac est nec odio ornare venenatis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi leo. Ut ac est nec odio ornare venenatis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</body>
</html>
Copy linkTweet thisAlerts:
@rnd_meMar 31.2010 — [code]
function findText(snippet) {
var newSourceCode, count=0;
newSourceCode= document.body.innerHTML;
re = /[^<][ws]*leo/gi;
newSourceCode= newSourceCode.replace(re,'FOO BAR');
document.body.innerHTML = newSourceCode;
}[/QUOTE]

couldn't that find/break attribute values?


edit/
here is a safer one i cooked up...

firebug test:
[CODE]
function rep(dis,dat){
var db=document.body, r=db.getElementsByTagName("*"), mx=r.length,
prop=db.innerText? "innerText":"textContent";
for(var i=0; i<mx;i++){
if(!r[i].getElementsByTagName("*").length)
r[i][prop]=r[i][prop].replace(dis,dat);
}
}


//firebug test:
rep("Fang","rnd me")
rep(/Resist[ws]+/g,"You've been assimilated");

[/CODE]
Copy linkTweet thisAlerts:
@FangMar 31.2010 — As far is I remember(5 year old script) it ignores anything within the tag.

If you have a better regexp please post.
Copy linkTweet thisAlerts:
@malborojonesauthorMar 31.2010 — Thanks both, I'll give them a try and see which one works best for me.

Malb
Copy linkTweet thisAlerts:
@iwpgApr 13.2010 — Hi everyone, I hate to post redundant coding questions, but javascript is like Chinese to me. Basically, I am running an Ajax/PHP combo that uses PHP to make a MYSQL query and returns the results live in the search box. The problem is that all ampersands are being returned as an HTML entity. E.G. [B]&[/B] returns [B]&amp;[/B] Is there a way to properly replace that code back to an ampersand?

Here is the javascript function that performs the retrieval of contents from PHP (PHP only returns text):

[CODE]//Called when the AJAX response is returned.
function handleSearchSuggest() {
if (searchReq.readyState == 4) {
var ss = document.getElementById('search_suggest')
ss.innerHTML = '';
var str = searchReq.responseText.split("n");
for(i=0; i < str.length - 1; i++) {
//Build our element string. This is cleaner using the DOM, but
//IE doesn't support dynamically added attributes.


var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
suggest += 'onmouseout="javascript:suggestOut(this);" ';
suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
suggest += 'class="suggest_link">' + str[i] + '</div>';
ss.innerHTML += suggest;
}
}
}[/CODE]


A working example of this issue can be found at: http://www.financeglobe.com/SocialNet/Markets/ and typing in abercrombie and then clicking on the suggestion.

Many thanks.
×

Success!

Help @malborojones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...