470,849 Members | 839 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

How to dinamically change the string in a find statement?

I have a script that opens a small window containing a button which
when clicked performs a find to the next occurence of a string. It
works well when the string is made of only one word or of consecutive
words.
Now I want it to find the next occurence of one of the words contained
in a list, ie. I want to dynamically change the string to find.
I failed to use a regular expression instead of a string in the find
statement.
Any idea of how I can do that?
Thanks.
Jul 23 '05 #1
4 1328
Yv*****************@bull.net (Yves-Alain NICOLLET) wrote in message news:<72**************************@posting.google. com>...
I have a script that opens a small window containing a button which
when clicked performs a find to the next occurence of a string. It
works well when the string is made of only one word or of consecutive
words.
Now I want it to find the next occurence of one of the words contained
in a list, ie. I want to dynamically change the string to find.
I failed to use a regular expression instead of a string in the find
statement.
Any idea of how I can do that?
Thanks.


To make myself clearer, is there a way to say:

find(a_word OR another_word OR yet_another_word OR etc) ?
Jul 23 '05 #2
Yves-Alain NICOLLET wrote:
Yv*****************@bull.net (Yves-Alain NICOLLET) wrote in message
news:<72**************************@posting.google. com>...


Please do not write attribution novels.
I have a script that opens a small window containing a button
Note the security restrictions for the window size:

http://devedge.netscape.com/manuals/2000/javascript/1.3/reference/window.html#1202731>
[...] Now I want it to find the next occurence of one of
the words contained in a list, ie. I want to dynamically change the
string to find. I failed to use a regular expression instead of a
string in the find statement. Any idea of how I can do that? Thanks.


To make myself clearer, is there a way to say:

find(a_word OR another_word OR yet_another_word OR etc) ?


Depends. There is the proprietary window.find() method which
returns `true' if unsuccessful, `false' otherwise. So you can
do consecutive searches:

function findWords()
{
var t;
if ((t = typeof window.find) == "function"
|| (t == object && window.find != null))
{
for (var i = 0, len = arguments.length; i < len; i++)
{
var s = arguments[i];
if (window.find(s))
{
return true; // or s if you like
}
}
}

return false; // no match
}

if (!findWords("a_word", "another_word", "yet_another_word", "etc"))
{
alert("No match for either"
+ "'a_word', 'another_word', 'yet_another_word' or 'etc'");
}

If you want two or more words to be matched and to be selected at
the same time, you need to parse the documents content. Dirty
proprietary quickhack:

document.body.innerHTML.replace(
/(a_word|another_word|yet_another_word|etc)/g,
'<span style="background-color:yellow; color:black">$1</span>');
HTH

PointedEars
Jul 23 '05 #3
Thomas 'Ingrid' Lahn wrote:
[...] There is the proprietary window.find() method which
returns `true' if unsuccessful, `false' otherwise. [...]


returns `true' if successful, `false' otherwise. [...]
PointedEars
Jul 23 '05 #4
Thomas 'Ingrid' Lahn wrote:
If you want two or more words to be matched and to be selected at
the same time, you need to parse the documents content. Dirty
proprietary quickhack:

document.body.innerHTML.replace(
/(a_word|another_word|yet_another_word|etc)/g,
'<span style="background-color:yellow; color:black">$1</span>');


This changes nothing really and "must be" "of course"[1]

document.body.innerHTML = document.body.innerHTML.replace(
/(a_word|another_word|yet_another_word|etc)/g,
'<span style="background-color:yellow; color:black">$1</span>');
PointedEars
___________
[1] Note it is still an evil quickhack.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard Cornford | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by Fabio Cavassini | last post: by
32 posts views Thread by Mateo | last post: by
reply views Thread by Chepre | last post: by
5 posts views Thread by srcp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.