Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > issue with eval and regular expression

Reply
Thread Tools

issue with eval and regular expression

 
 
graphicsxp
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
graphicsxp
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-11-2009
On Jun 11, 4:18*pm, graphicsxp <(E-Mail Removed)> 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

 
Reply With Quote
 
Michael J. Ryan
Guest
Posts: n/a
 
      06-13-2009
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!
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-13-2009
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catch regular expression errors with eval David Liang Perl Misc 1 09-21-2009 03:22 PM
error with regular expression inside eval block Barun Singh Ruby 4 01-29-2009 12:55 PM
Different behavior between eval "07" and eval "08" Liang Wang Perl Misc 8 02-02-2008 08:31 PM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments