issue with eval and regular expression

Discussion in 'Javascript' started by graphicsxp, Jun 11, 2009.

  1. graphicsxp

    graphicsxp Guest

    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
    graphicsxp, Jun 11, 2009
    #1
    1. Advertising

  2. 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
    Thomas 'PointedEars' Lahn, Jun 11, 2009
    #2
    1. Advertising

  3. graphicsxp

    Erwin Moller Guest

    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
    Erwin Moller, Jun 11, 2009
    #3
  4. graphicsxp

    graphicsxp Guest

    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
    graphicsxp, Jun 11, 2009
    #4
  5. 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
    Thomas 'PointedEars' Lahn, Jun 11, 2009
    #5
  6. graphicsxp

    VK Guest

    On Jun 11, 4:18 pm, graphicsxp <> 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 :)
    VK, Jun 11, 2009
    #6
  7. 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!
    Michael J. Ryan, Jun 13, 2009
    #7
  8. graphicsxp

    Evertjan. Guest

    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)
    Evertjan., Jun 13, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,290
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Barun Singh
    Replies:
    4
    Views:
    137
    Robert Klemme
    Jan 29, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    131
    Ben Morrow
    Feb 2, 2008
  5. David Liang

    Catch regular expression errors with eval

    David Liang, Sep 21, 2009, in forum: Perl Misc
    Replies:
    1
    Views:
    111
    Uri Guttman
    Sep 21, 2009
Loading...

Share This Page