473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

javascript regex test fails

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.lengt h; 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

May 19 '07 #1
9 2384
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 19 '07 #2
On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


I didn't even think of that. How can I put a variable in there to
search?
May 19 '07 #3
wrote on 19 mei 2007 in comp.lang.javascript:
On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
> 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.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 19 '07 #4
On May 19, 1:57 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
wrote on 19 mei 2007 in comp.lang.javascript:
On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan, that worked beautifully! Thanks for the help.

Dave

May 19 '07 #5
In comp.lang.javascript message <11**********************@u30g2000hsc.go
oglegroups.com>, Sat, 19 May 2007 08:48:48, he******@gmail.com 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.lengt h; 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. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
May 19 '07 #6
On May 19, 3:29 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In comp.lang.javascript message <1179589728.556362.297...@u30g2000hsc.go
oglegroups.com>, Sat, 19 May 2007 08:48:48, hende...@gmail.com 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.lengt h; 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. ?...@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr 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

May 20 '07 #7
On May 19, 8:06 pm, hende...@gmail.com wrote:
On May 19, 3:29 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In comp.lang.javascript message <1179589728.556362.297...@u30g2000hsc.go
oglegroups.com>, Sat, 19 May 2007 08:48:48, hende...@gmail.com 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.lengt h; 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. ?...@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr 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.

May 20 '07 #8
On May 19, 6:06 pm, hende...@gmail.com wrote:
//re = /\w+\s/g;
re=/.+?\s/g;
str = "fee fi fo fum";
myArray = str.match(re);
alert(myArray);
May 21 '07 #9
On May 20, 11:07 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On May 19, 6:06 pm, hende...@gmail.com wrote:
//re = /\w+\s/g;

re=/.+?\s/g;

worked like a charm! Thanks for the help.

May 22 '07 #10

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

Similar topics

2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
9
by: gleverett | last post by:
I have been searching the 'Net, and I can't find the right solution here. I am writing some PHP pages that utilize some Javascript. The script works in Mozilla/Netscape, but fails in IE. I don't...
1
by: tdmailbox | last post by:
I have the following regular expression. It works fine if the regex code returns a match. However if not the .match code fails. How can I code this so that it skips the match if the regular...
0
by: Chris McKenzie | last post by:
Hi, I'm using IE 6, and I'm doing some RegEx replacement on the client. Here's my code: regExStr = "\\d{2}:\\d{2}"; // where pattern matches ##:## regExStr += "(?=\\s)"; // and the next...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
0
by: angshuman.agarwal | last post by:
Hi, I have a owner drawn combo box (using .Net 2.0 Framework). I have written the Nunit test for the same. I am setting the AutoCompleteSource Property (AutoCompleteSource.CustomSource) and...
60
by: marss | last post by:
Maybe anyone know good free online JavaScript knowledge test? This not exactly a system for testing online required - it may be simply list of questions with variants of answers (I have to prepare...
9
by: kummu4help | last post by:
can anyone give me a regex to validate the password with following conditions hope i am clear. i tried with ctype_alnum() function in php but it is accepting if all characters or either...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.