/    Sign up×
Community /Pin to ProfileBookmark

Remove text using JS

Consider I ahve the following text:

<h4>The quick brown fox jumps over the lazy dog and feels as if he were in the seventh heaven. Why does it jump quick over a dog?</h4>

Now is it possible to use Javascript to scan this peice of text and remove certain words?

If I want to remove “[COLOR=”Green”]and feels[/COLOR]” as well as “[COLOR=”Green”]quick[/COLOR]

I should end up with this:

<h4>The brown fox jumps over the lazy dog as if he were in the seventh heaven. Why does it jump over a dog?</h4>

Is this possible? If so can someone provide a working example.

Many thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@DracoMerestAug 24.2011 — Yes, it is very possible. You need to learn something about Regular Expressions.

aka RegEx and RegExp. There are thousands of tutorials when you search google.

&lt;html&gt;
&lt;script language="javascript"&gt;

inpStr="The quick brown fox jumps over the lazy dog and feels as if he were in the seventh heaven. Why does it jump quick over a dog?"

rAry=new Array("and feels","quick");

for(i=0;i&lt;rAry.length;i++)
inpStr=inpStr.replace(RegExp(rAry[i],"g"),"");

document.write(inpStr);

&lt;/script&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@amean_nauthorAug 24.2011 — Thank you for your reply.

And yes, Google is my friend. I tried your code, and it works. However it only works on the text you declare in the var and spits it out without whatever has been specified. What If I wanted the script to scan a page which all ready has the text on it. I.E scan the page and delete/replace certain words.

Thanks ?
×

Success!

Help @amean_n 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...