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
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)
![]() |
0 |
![]() |
On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> wrote: > 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?
![]() |
0 |
![]() |
wrote on 19 mei 2007 in comp.lang.javascript: > On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> wrote: >> 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)
![]() |
0 |
![]() |
On May 19, 1:57 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> wrote: > wrote on 19 mei 2007 in comp.lang.javascript: > > > > > On May 19, 1:45 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> wrote: > >> 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
![]() |
0 |
![]() |
In comp.lang.javascript message <1179589728.556362.297530@u30g2000hsc.go oglegroups.com>, Sat, 19 May 2007 08:48:48, hendedav@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.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. ?@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.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
![]() |
0 |
![]() |
On May 19, 3:29 pm, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote: > 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.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. ?...@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.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
![]() |
0 |
![]() |
On May 19, 8:06 pm, hende...@gmail.com wrote: > On May 19, 3:29 pm, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote: > > > > > 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.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. ?...@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.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.
![]() |
0 |
![]() |
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); >
![]() |
0 |
![]() |
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.
![]() |
0 |
![]() |