javascript regex test fails

H

hendedav

Gang,

I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:



for (var j=0; j<document.getElementById('cmbList').options.length; j+
+) {
if (document.getElementById('cmbList').options[j].value ==
object.firstChild.data) { strAnswer = "specific"; break; }

alert('does .' +
document.getElementById('cmbList').options[j].value + '. contain .' +
object.firstChild.data + '.');

if (/
^object.firstChild.data/.test(document.getElementById('cmbList').options[j].value))
{
alert('in matches');
strAnswer = "child";
break;
}
}


I know the everything is working except the test function due to the
alert statement and the output generated. The two values being
compared contain directory paths (ie '/some/dir') and I can see (by
the alert statement) while its comparing values that some directories
are child of the parent directory, yet the .test function fails. Is
there a better way to perform this loop or is there an error with my
test statement?

Thanks,

Dave
 
E

Evertjan.

wrote on 19 mei 2007 in comp.lang.javascript:
I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
[..]

if (/
^object.firstChild.data/.test(document.getElementById('cmbList').option
s[j].value))

/^object.firstChild.data/

I doubt you want to search on this litteral text,
where the dots are tests for any single character.
 
H

hendedav

wrote on 19 mei 2007 in comp.lang.javascript:


I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
[..]

if (/
^object.firstChild.data/.test(document.getElementById('cmbList').option
s[j].value))

/^object.firstChild.data/

I doubt you want to search on this litteral text,
where the dots are tests for any single character.



I didn't even think of that. How can I put a variable in there to
search?
 
E

Evertjan.

wrote on 19 mei 2007 in comp.lang.javascript:
wrote on 19 mei 2007 in comp.lang.javascript:


I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
[..]

if (/
^object.firstChild.data/.test(document.getElementById('cmbList').opt
ion s[j].value))

/^object.firstChild.data/

I doubt you want to search on this litteral text,
where the dots are tests for any single character.

[please do not quote signatures on usenet]
I didn't even think of that. How can I put a variable in there to
search?

var re = new RegExp('^'+object.firstChild.data,'')
var s = document.getElementById('cmbList').options[j].value
if (re.test(s))
alert('Hi')

not tested as such
 
H

hendedav

wrote on 19 mei 2007 in comp.lang.javascript:


wrote on 19 mei 2007 in comp.lang.javascript:
I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
[..]
if (/
^object.firstChild.data/.test(document.getElementById('cmbList').opt
ion s[j].value))
/^object.firstChild.data/
I doubt you want to search on this litteral text,
where the dots are tests for any single character.

[please do not quote signatures on usenet]


I didn't even think of that. How can I put a variable in there to
search?

var re = new RegExp('^'+object.firstChild.data,'')
var s = document.getElementById('cmbList').options[j].value
if (re.test(s))
alert('Hi')

not tested as such


Evertjan, that worked beautifully! Thanks for the help.

Dave
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sat, 19 May 2007 08:48:48, (e-mail address removed) posted:
I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:



for (var j=0; j<document.getElementById('cmbList').options.length; j+
+) {
if (document.getElementById('cmbList').options[j].value ==
object.firstChild.data) { strAnswer = "specific"; break; }

alert('does .' +
document.getElementById('cmbList').options[j].value + '. contain .' +
object.firstChild.data + '.');

if (/
^object.firstChild.data/.test(document.getElementById('cmbList').option
s[j].value))
{
alert('in matches');
strAnswer = "child";
break;
}
}


I know the everything is working except the test function due to the
alert statement and the output generated. The two values being
compared contain directory paths (ie '/some/dir') and I can see (by
the alert statement) while its comparing values that some directories
are child of the parent directory, yet the .test function fails. Is
there a better way to perform this loop or is there an error with my
test statement?

There, document.getElementById('cmbList').options.length appears to
be evaluated once for each option and once for the non-next one. Set
that into a simple variable before the loop, and use that in the loop
control.

And document.getElementById('cmbList') is repeatedly evaluated in
the first "if". Save that in a variable, and use it there and above.

And document.getElementById('cmbList').options[j].value is called
twice. Save that ...


That which the RegExp literal /^object.firstChild.data/ contains
seems unlikely to be what you want to be there. It will match the
beginning of a string object.firstChild.data (with dot wild) and
has nothing to do with, for example, the data element of the first child
of object.

Create simpler code to develop that you are trying to do there. You may
well need something more like

RE = new RegExp(object.firstChild.data)
if RE.test(valueJ) { ... }


Use of "object" as an identifier mat well be legitimate, but it can
confuse. Just re-spell it so that it looks more different from
"Object".

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
H

hendedav

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sat, 19 May 2007 08:48:48, (e-mail address removed) posted:


I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
for (var j=0; j<document.getElementById('cmbList').options.length; j+
+) {
if (document.getElementById('cmbList').options[j].value ==
object.firstChild.data) { strAnswer = "specific"; break; }
alert('does .' +
document.getElementById('cmbList').options[j].value + '. contain .' +
object.firstChild.data + '.');
if (/
^object.firstChild.data/.test(document.getElementById('cmbList').option
s[j].value))
{
alert('in matches');
strAnswer = "child";
break;
}
}
I know the everything is working except the test function due to the
alert statement and the output generated. The two values being
compared contain directory paths (ie '/some/dir') and I can see (by
the alert statement) while its comparing values that some directories
are child of the parent directory, yet the .test function fails. Is
there a better way to perform this loop or is there an error with my
test statement?

There, document.getElementById('cmbList').options.length appears to
be evaluated once for each option and once for the non-next one. Set
that into a simple variable before the loop, and use that in the loop
control.

And document.getElementById('cmbList') is repeatedly evaluated in
the first "if". Save that in a variable, and use it there and above.

And document.getElementById('cmbList').options[j].value is called
twice. Save that ...

That which the RegExp literal /^object.firstChild.data/ contains
seems unlikely to be what you want to be there. It will match the
beginning of a string object.firstChild.data (with dot wild) and
has nothing to do with, for example, the data element of the first child
of object.

Create simpler code to develop that you are trying to do there. You may
well need something more like

RE = new RegExp(object.firstChild.data)
if RE.test(valueJ) { ... }

Use of "object" as an identifier mat well be legitimate, but it can
confuse. Just re-spell it so that it looks more different from
"Object".

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6
FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.



Thanks for the input as well. I have another issue. I can't figure
regex's out apparently. The below is a very over simplified example
of what I am trying to accomplish. Take for instance this code:


//re = /\w+\s/g;
re = /.+\s/;
str = "fee fi fo fum";
myArray = str.match(re);
alert(myArray);


if I uncomment the first line (re = /\w+\s/g;) and comment out the
second line (re = /.+\s/;) and run the script I get the following
results:

fee ,fi ,fo ,fum

stored in an array. So far so good. I would like to accomplish the
same goal using the period wildcard (.), so I commentted out the first
line (re = /\w+\s/g;) and uncommented the second (re = /.+\s/;). The
results were an array of 1 containing the entire line. Why does the
regular expression evalute to this? Shouldn't it match *anything* up
to a space since that is the terminating delimiter (in the example
from above, it should only return "fee ". Also note, that I didn't
give the "g" at the end for a global search. How could I get my
logic to work using the . wildcard in this example?

Thanks,

Dave
 
H

hendedav

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sat, 19 May 2007 08:48:48, (e-mail address removed) posted:
I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
for (var j=0; j<document.getElementById('cmbList').options.length; j+
+) {
if (document.getElementById('cmbList').options[j].value ==
object.firstChild.data) { strAnswer = "specific"; break; }
alert('does .' +
document.getElementById('cmbList').options[j].value + '. contain .' +
object.firstChild.data + '.');
if (/
^object.firstChild.data/.test(document.getElementById('cmbList').option
s[j].value))
{
alert('in matches');
strAnswer = "child";
break;
}
}
I know the everything is working except the test function due to the
alert statement and the output generated. The two values being
compared contain directory paths (ie '/some/dir') and I can see (by
the alert statement) while its comparing values that some directories
are child of the parent directory, yet the .test function fails. Is
there a better way to perform this loop or is there an error with my
test statement?
There, document.getElementById('cmbList').options.length appears to
be evaluated once for each option and once for the non-next one. Set
that into a simple variable before the loop, and use that in the loop
control.
And document.getElementById('cmbList') is repeatedly evaluated in
the first "if". Save that in a variable, and use it there and above.
And document.getElementById('cmbList').options[j].value is called
twice. Save that ...
That which the RegExp literal /^object.firstChild.data/ contains
seems unlikely to be what you want to be there. It will match the
beginning of a string object.firstChild.data (with dot wild) and
has nothing to do with, for example, the data element of the first child
of object.
Create simpler code to develop that you are trying to do there. You may
well need something more like
RE = new RegExp(object.firstChild.data)
if RE.test(valueJ) { ... }
Use of "object" as an identifier mat well be legitimate, but it can
confuse. Just re-spell it so that it looks more different from
"Object".
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6
FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Thanks for the input as well. I have another issue. I can't figure
regex's out apparently. The below is a very over simplified example
of what I am trying to accomplish. Take for instance this code:

//re = /\w+\s/g;
re = /.+\s/;
str = "fee fi fo fum";
myArray = str.match(re);
alert(myArray);

if I uncomment the first line (re = /\w+\s/g;) and comment out the
second line (re = /.+\s/;) and run the script I get the following
results:

fee ,fi ,fo ,fum

stored in an array. So far so good. I would like to accomplish the
same goal using the period wildcard (.), so I commentted out the first
line (re = /\w+\s/g;) and uncommented the second (re = /.+\s/;). The
results were an array of 1 containing the entire line. Why does the
regular expression evalute to this? Shouldn't it match *anything* up
to a space since that is the terminating delimiter (in the example
from above, it should only return "fee ". Also note, that I didn't
give the "g" at the end for a global search. How could I get my
logic to work using the . wildcard in this example?

Thanks,

Dave


come on regexp guru's help me out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,699
Members
45,489
Latest member
SwethaJ

Latest Threads

Top