473,569 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search/replace patterns in web pages?

Hi,

I need to search and replace patterns in web pages, but I
can't find a way even after reading the ad hoc chapter in New Rider's
"Inside JavaScript".

Here's what I want to do:

function filter() {
var items = new Array("John", "Jane");

for (x = 0; x < items.length; x++) {
//Doesn't work
pattern = '/' + items[x] + '/';
//Doesn't work either
document.body = document.body.r eplace(pattern, "IGNORED");
}

ie., create an array of items to look for in the BODY section of the
page, and if any item exists, replace the item with IGNORED.

Anyone knows how to do this?

Thank you
JD.
Jul 20 '05 #1
4 10185
Jane Doe <ja******@acme. com> writes:
Hi,

I need to search and replace patterns in web pages, but I
can't find a way even after reading the ad hoc chapter in New Rider's
"Inside JavaScript".

Here's what I want to do:

function filter() {
var items = new Array("John", "Jane");

for (x = 0; x < items.length; x++) {
//Doesn't work
pattern = '/' + items[x] + '/';
This builds a string. (Make pattern a local variable with the "var" operator,
no need to have it global).
//Doesn't work either
document.body = document.body.r eplace(pattern, "IGNORED");


The object document.body is a DOM Node, not a text string.
What you can do, in some browsers, is to work on
document.body.i nnerHTML.

Also, change "pattern" to "new RegExp(items[x],'')" in this line. Then
you have created a regular expression with the name as content.

There is no need to run through all the items on at a time.
You can replace the entire for loop with

document.body.i nnerHTML =
document.body.i nnerHTML.replac e(new RegExp(items.jo in("|"),""),"IG NORED");

(This way, the regualr expression becomes "John|Jane" . Since you replace
them with the same string, you can just match them at the same time.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
On 09 Sep 2003 14:03:34 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:
document.body.i nnerHTML =
document.body.i nnerHTML.replac e(new RegExp(items.jo in("|"),""),"IG NORED");


Thx a bunch Lasse for the prompt answer :-) It looks like a much
better solution, although I'll still have to find out the following:

1. innerHTML only seems to work in IE. Doesn't work with Opera 5 and
might not work with Netscape

2. Only the first occurence of the pattern is replace, ie. if I have
(John|Jane), and those items both appear in the page, only the first
occurence is replaced (the second is ignored). I assume I need to add
/g somewhere to tell JS to search & replace _all_ occurences

3. I'm actually parsing rows in a table, so need to construct a more
complicated search pattern than the one I gave to get started. The
goal is to replace any row that contains any of the items into an
empty row (ie.
<tr><td>&nbsp ;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>).

FWIW, here's what I'd like to do:

---------
function clean() {
var items = new Array("John", "Jane");
document.body.i nnerHTML = document.body.i nnerHTML.replac e(new
RegExp(items.jo in("|"),""),"IG NORED");
}

[...]

<body onload='clean() ()'>

<table>
<tr>
<td bgcolor="#FFFFF F" ><a
href="forum.php ?forum=myforum& m=123">Title</a></td>
<td bgcolor="#FFFFF F">John</td>
<td bgcolor="#FFFFF F">10</td>
<td bgcolor="#FFFFF F">Posted 13 sept</td>
</tr>
<tr>
<td bgcolor="#FFFFF F" ><a
href="forum.php ?forum=myforum& m=124">Title</a></td>
<td bgcolor="#FFFFF F">Jane</td>
<td bgcolor="#FFFFF F">2</td>
<td bgcolor="#FFFFF F">Posted 12 sept</td>
</tr>
</table>

---------

If you have any idea or sample code on the Net swhere, I'm interested
:-)

Thx again for your help
JD.
Jul 20 '05 #3
Jane Doe <ja******@acme. com> writes:
On 09 Sep 2003 14:03:34 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:
document.body.i nnerHTML =
document.body.i nnerHTML.replac e(new RegExp(items.jo in("|"),""),"IG NORED");
Thx a bunch Lasse for the prompt answer :-) It looks like a much
better solution, although I'll still have to find out the following:

1. innerHTML only seems to work in IE. Doesn't work with Opera 5 and
might not work with Netscape


It works in IE 4+, Opera 7 and Mozilla. Perhas a few other recent
browsers. Any older browsers are out.

On the other hand, Netscape 4 and Opera 6 will not allow you to change
the contents of the page at all, after it is loaded. So there is no
method that works there.

If you can ignore IE 4, I would prefer to use DOM methods, traversing
the DOM tree and changing the text in the text nodes.

2. Only the first occurence of the pattern is replace, ie. if I have
(John|Jane), and those items both appear in the page, only the first
occurence is replaced (the second is ignored). I assume I need to add
/g somewhere to tell JS to search & replace _all_ occurences
Doh. Yes, the place to add the "g" is in the second argument to RegExp
(currently an empty string, make it "g", and perhaps even "gi").
Also notice that you match even inside words, so Johnson becomes
IGNOREDson. You can fix that, by making the regular expression

new RegExp("\\b("+i tems.join("|")+ ")\\b","gi" );

The "\b" matches the boundary between a word character and a non-word
character, so it won't match after "John" in "Johnson".
3. I'm actually parsing rows in a table, so need to construct a more
complicated search pattern than the one I gave to get started.
It is sometimes easier to split the problem into more than one regular
expression. E.g., one to find a table row, another to test whether
it contains the forbidden words. You can alway combine them, they might
just be horribly much bigger.
function clean() {


Ok. If we only aim at newer browsers, try this:

function clean() {
var body = document.body.i nnerHTML;
var itemRE = new RegExp("\\b("+i tems.join("|")+ ")\\b","gi" );
body = body.replace(/<tr(.|\s)*?<\/tr>/gi,function(row ) {
if (row.match(item RE)) {
return "";
} else {
return row;
}
});
document.body.i nnerHTML = body;
}

it replace each table row (from "<tr" to "</tr>") with either
itself or the empty string, depending on whether the row
contains the words in the "items" array.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
On 09 Sep 2003 14:56:54 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:
Ok. If we only aim at newer browsers, try this:


You're awesome :-) Works like a charm. I owe you dinner next time
you're in town.

Thx again
JD.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3520
by: CharitiesOnline | last post by:
Hello, I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be depending on the number of results returned from my search and how many records I have set it to display per page. All great so far, HOWEVER..... When you click the next link to see the...
67
5968
by: Sandy.Pittendrigh | last post by:
Here's a question I don't know the answer to: I have a friend who makes very expensive, hand-made bamboo flyrods. He's widely recognized (in the fishing industry) as one of the 3-5 'best' rod makers in the world. He gets (sic) close to $5000 per custom made flyrod. A surprising number of people buy these fishing rods and never use...
4
7098
by: Ben Fidge | last post by:
Hi What is the most efficient way to gather a list of files under a given folder recursively with the ability to specify exclusion file masks? For example, say I wanted to get a list of all files under \Program Files and it's subdirectories that meet the following criteria:
32
14783
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
8245
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}") Thanks, Craig
4
4824
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace...
1
7519
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is...
2
1990
by: Pallav singh | last post by:
Hi Kindly Provide me Link about Concurrency patterns in C++ Thanks Pallav Singh
5
2425
by: manish | last post by:
HI I want a jawascript that will search pages from only my subdomain website eg. abc.efg.com can anyone help me please give the exact jawascript as I am not an expert programmer please if possible email it to india.manish.wagh@gmail.com awaiting Manish wagh
0
7704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7620
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7985
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3659
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.