issue with eval and regular expression

G

graphicsxp

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
 
T

Thomas 'PointedEars' Lahn

graphicsxp said:
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
 
E

Erwin Moller

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
 
G

graphicsxp

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
 
T

Thomas 'PointedEars' Lahn

graphicsxp said:
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
 
V

VK

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 :)
 
M

Michael J. Ryan

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.
 
E

Evertjan.

Michael J. Ryan wrote on 13 jun 2009 in comp.lang.javascript:
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);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top