|
From: Gary Wardell on 24 Apr 2008 11:55 Hi, I wanted to check if this was correct be behavior before reporting it as a bug in FireFox. Given this function: function test() { var re; re = /^(.+)\@(.+?)$/gi; if (re.exec("test(a)example.com") != null) { alert("Match"); } else { alert("Null"); } return; } Should it alternately between displaying "Match" and "Null" each time it's called? I would think that since the regex and the string are always the same if should always display "Match"??? What I am seeing is that it alternates. If this is proper behavior, why? Gary
From: Martin Honnen on 24 Apr 2008 12:08 Gary Wardell wrote: > Should it alternately between displaying "Match" and "Null" each time it's > called? > > I would think that since the regex and the string are always the same if > should always display "Match"??? > > What I am seeing is that it alternates. > > If this is proper behavior, why? Use function test() { var re; re = new RegExp('^(.+)\\@(.+?)$', 'gi'); if (re.exec("test(a)example.com") != null) { alert("Match"); } else { alert("Null"); } return; } or live with the ECMAScript specification insisting on creating one regular expression object for each regular expression literal, even if it is inside of a function as a local variable: "7.8.5 Regular Expression Literals A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the literal produces a reference to that object; it does not create a new object." So what happens is that in your code the regular expression literal creates one regular expression object with the g flag when the program code is scanned, and then when the function is executed that regular expression object is referenced and because of the g flag every second exec starts after the last match and does not find anything. -- Martin Honnen http://JavaScript.FAQTs.com/
From: Bjoern Hoehrmann on 24 Apr 2008 12:10 * Gary Wardell wrote in comp.lang.javascript: >I wanted to check if this was correct be behavior before reporting it as a >bug in FireFox. > >Given this function: > >function test() >{ > var re; > > re = /^(.+)\@(.+?)$/gi; > if (re.exec("test(a)example.com") != null) > { > alert("Match"); > } else { > alert("Null"); > } > > return; >} > >Should it alternately between displaying "Match" and "Null" each time it's >called? A quick reading of ECMA-262 15.10.6.2 suggests just that, yes. Note that you are using the 'global' ('g') property so; if you don't, you get the result you expect. -- Bj�rn H�hrmann � mailto:bjoern(a)hoehrmann.de � http://bjoern.hoehrmann.de Weinh. Str. 22 � Telefon: +49(0)621/4309674 � http://www.bjoernsworld.de 68309 Mannheim � PGP Pub. KeyID: 0xA4357E78 � http://www.websitedev.de/
From: Evertjan. on 24 Apr 2008 12:10 Gary Wardell wrote on 24 apr 2008 in comp.lang.javascript: > Hi, > > I wanted to check if this was correct be behavior before reporting it > as a bug in FireFox. > > Given this function: > > function test() > { > var re; > > re = /^(.+)\@(.+?)$/gi; > if (re.exec("test(a)example.com") != null) > { > alert("Match"); > } else { > alert("Null"); > } > > return; >} > > Should it alternately between displaying "Match" and "Null" each time > it's called? > > I would think that since the regex and the string are always the same > if should always display "Match"??? > > What I am seeing is that it alternates. > > If this is proper behavior, why? 1 You should use .test() to test, that's where test() is for. 2 in exec() the ponter of the defined regex object is kept from the last call, this could be your problem. > re = /^(.+)\@(.+?)$/gi; 3 @ does not need to be escaped, the () are unneccessary, the g flag is not used, the i flag is not used. Try: if ( /^.+@.+$/.test('test(a)example.com') ) alert('Match') else alert('No match'); -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Gary Wardell on 24 Apr 2008 12:20 Hi Martin, Thanks for the explanation. Given the regex ^(.+)\@(.+?)$ I'm wondering if a really need the "gi", since it's matching the whole string and it doesn't really care about case? Is "new" the preferred way to create regex objects or is it just better in this case? I'm a little bit new to both regex and JavaScript. Gary
|
Next
|
Last
Pages: 1 2 Prev: Maintain a Div's Scroll Position After refresh Next: Tooltip text not showing |