Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   issue with eval and regular expression (http://www.velocityreviews.com/forums/t938807-issue-with-eval-and-regular-expression.html)

graphicsxp 06-11-2009 11:12 AM

issue with eval and regular expression
 
Hi,

I use eval to convert a string to a regular expression and I use exec
to find whether a string matches the regexp or not:

eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")

should return an array, prooving it is matching.

This method works really well with all the regular expression I've
worked with so far. But not with the one above !

In fact it's very strange because if I do :

var myRegExprString = "/(\\d)+(\\.)+(\\d)/";
eval(myRegExprString).exec("1.3")

This returns null. However if I do :

eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")

that returns an array ! I can't see the difference between passing a
string variable or just a string.... Besides it only occurs with this
particular regexpr, not with others !

Can someone help ?

Thanks

Thomas 'PointedEars' Lahn 06-11-2009 11:39 AM

Re: issue with eval and regular expression
 
graphicsxp wrote:
> I use eval to convert a string to a regular expression


Don't. Use

RegExp("...", "...")

or

new RegExp("...", "...")

instead.

> [...] and I use exec to find whether a string matches the regexp or not:


Don't. Use

regexp.test("...")

instead, unless you need the matched substrings.

> In fact it's very strange because if I do :
>
> var myRegExprString = "/(\\d)+(\\.)+(\\d)/";
> eval(myRegExprString).exec("1.3")


var myRegExprString = /(\d)+(\.)+(\d)/;
myRegExprString.test("1.3")

> This returns null.


No, it doesn't. According to Firebug, the result of the program is

["1.3", "1", ".", "3"]

> However if I do :
>
> eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")
>
> that returns an array !


Works as designed.

> I can't see the difference between passing a string variable or just
> a string.... Besides it only occurs with this particular regexpr,
> not with others !


Probably there is a typo or something along

<?php
echo 'eval("/(\\d)+(\\.)+(\\d)/").exec("1.3");';
?>

In any case, you should improve your approach as suggested.

When posting to comp.lang.javascript, please take heed of
<http://jibbering.com/faq/#posting>.


PointedEars

Erwin Moller 06-11-2009 11:41 AM

Re: issue with eval and regular expression
 
graphicsxp schreef:
> Hi,
>
> I use eval to convert a string to a regular expression and I use exec
> to find whether a string matches the regexp or not:
>
> eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")
>
> should return an array, prooving it is matching.
>
> This method works really well with all the regular expression I've
> worked with so far. But not with the one above !
>
> In fact it's very strange because if I do :
>
> var myRegExprString = "/(\\d)+(\\.)+(\\d)/";
> eval(myRegExprString).exec("1.3")
>
> This returns null. However if I do :
>
> eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")
>
> that returns an array ! I can't see the difference between passing a
> string variable or just a string.... Besides it only occurs with this
> particular regexpr, not with others !
>
> Can someone help ?
>
> Thanks


Hi,

Why are you evalling your string that contains the regex?
The following is good JavaScript:
var myPattern = /(\d)+(\.)+(\d)/g;

or

var myPattern = new RegExp("(\\d)+(\\.)+(\\d)","g");

I am not sure if your approach adds problems, but I would avoid it anyway.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

graphicsxp 06-11-2009 12:18 PM

Re: issue with eval and regular expression
 
Hi both,

The reason I don't use RegExp is because there was a performance issue
with certain expressions. However using eval, it's fine.
It could be that my regexp was not very optimised, who knows..

>no, it doesn't. According to Firebug, the result of the program is
>["1.3", "1", ".", "3"]


Fair enough, that's what I'd expect too, but the bottom line is that
it doesn't work. Visual Studio returns null. It could be that it's
only in IE as I haven't tested with FF.

I've changed the regexp to : /([0-9])+(.)+([0-9])/

And it's working now. I believe somehow the \\ were responsible for
the issue. I don't have an explanation though...

Thanks

Thomas 'PointedEars' Lahn 06-11-2009 12:52 PM

Re: issue with eval and regular expression
 
graphicsxp wrote:
> The reason I don't use RegExp is because there was a performance issue
> with certain expressions. However using eval, it's fine.


As eval() evaluates any ECMAScript Program, while RegExp() evaluates only
regular expression syntax (and therefore uses a much more restricted
grammar), that appears to be highly unlikely.

> It could be that my regexp was not very optimised, who knows..


Probably.

>> no, it doesn't. According to Firebug, the result of the program is
>> ["1.3", "1", ".", "3"]

>
> Fair enough, that's what I'd expect too, but the bottom line is that
> it doesn't work. Visual Studio returns null. It could be that it's
> only in IE as I haven't tested with FF.


WFM in JScript 5.6.6626 (IE 6.0.2800.1106).

> I've changed the regexp to : /([0-9])+(.)+([0-9])/


That would match one or more occurrences of any character except newline
between the decimal digits.

> And it's working now.


By coincidence.


PointedEars

VK 06-11-2009 01:13 PM

Re: issue with eval and regular expression
 
On Jun 11, 4:18*pm, graphicsxp <graphic...@googlemail.com> wrote:
> Hi both,
>
> The reason I don't use RegExp is because there was a performance issue
> with certain expressions. However using eval, it's fine.
> It could be that my regexp was not very optimised, who knows..
>
> >no, it doesn't. *According to Firebug, the result of the program is
> >["1.3", "1", ".", "3"]

>
> Fair enough, that's what I'd expect too, but the bottom line is that
> it doesn't work. Visual Studio returns null. It could be that it's
> only in IE as I haven't tested with FF.
>
> I've changed the regexp to : /([0-9])+(.)+([0-9])/
>
> And it's working now. I believe somehow the \\ were responsible for
> the issue. I don't have an explanation though...
>
> Thanks


....
var myRegExprString = "/(\\d)+(\\.)+(\\d)/";
alert(eval(myRegExprString).exec("1.3"));
....

IE6, Windows XP SP3 - "1.3,1,.,3" // OK
IE8 X-UA-Compatible 7, Windows Vista SP1 - "1.3,1,.,3" // OK
IE8 Windows Vista SP1 - "1.3,1,.,3" // OK

This way it is not a browser issue but a programming environment
issue, you may check it yourself by handmaking the test page. Maybe
some weird preprocessor setting in Visual Studio? It is also possible
that you are being a very bad boy :-) so not placing the appropriate
META Content-Type tag in your page head section and atop of it View >
Encoding > Auto-Select is selected in IE: thus you are hitting the
unfamous IE's "Korean issue". If neither of both then your computer
is possessed, take it to the chirch ASAP :-)


Michael J. Ryan 06-13-2009 05:36 AM

Re: issue with eval and regular expression
 
On 6/11/2009 4:12 AM, graphicsxp wrote:
> Hi,
>
> I use eval to convert a string to a regular expression and I use exec
> to find whether a string matches the regexp or not:
>
> eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")


This is a lot of unneeded overhead when (/(\d)+(\.)+(\d)/).exec("1.3") would
work without the string evaluation, and the extra, unneeded escaping... and
works.

--
Michael J. Ryan - http://tracker1.info/

.... B5: The Council, the Council can go to hell. And the emergency session can
go to hell. And you, you can go to hell too. I wouldn't want you to feel left out!

Evertjan. 06-13-2009 06:59 AM

Re: issue with eval and regular expression
 
Michael J. Ryan wrote on 13 jun 2009 in comp.lang.javascript:

> On 6/11/2009 4:12 AM, graphicsxp wrote:
>> Hi,
>>
>> I use eval to convert a string to a regular expression and I use exec
>> to find whether a string matches the regexp or not:
>>
>> eval("/(\\d)+(\\.)+(\\d)/").exec("1.3")

>
> This is a lot of unneeded overhead when
> (/(\d)+(\.)+(\d)/).exec("1.3") would work without the string
> evaluation, and the extra, unneeded escaping... and works.


1 No reason for the outer () and all inner () in this regex.

2 When regex testing use test(),
not exec() or match()
[as you could make mistakes there more eaily].

trueFalse = /\d+\.+\d/.test('1.3');

however I suppose this is ment:

trueFalse = /\d+\.\d+/.test('1.3');

or even better:

trueFalse = /\d+\.?\d*/.test('1.3');

===================

however why not make a general function:

function stringIsNumeric(s) {
return 1*s == s;
};

alert(stringIsNumeric('1.3'))
alert(stringIsNumeric('1.3a'))

===================

or:

function stringIsNumeric(s) {
return !isNaN(s);
};




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


All times are GMT. The time now is 12:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.