regexp not matching variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel McLaughlin

    regexp not matching variable

    I have the following code that matches just fine.

    var re;
    re = /23-Apr-2004/gi;
    alert(re.test(d ocument.all['thebody'].innerHTML));

    However, when I attempt it with a variable containing the same characters it
    thows the error
    "Object doesn't support this property or method" on the test line below.

    var months = new
    Array('Jan','Fe b','Mar','Apr', 'May','Jun','Ju l','Aug','Sep', 'Oct','Nov','De c
    ');
    var now = new Date();
    var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
    now.getYear();
    today = "/"+today+"/gi";
    var re;
    re = today;
    alert(re.test(d ocument.all['thebody'].innerHTML));

    Can anyone tell me why this isn't working and perhaps a way around it?


  • Mick White

    #2
    Re: regexp not matching variable

    Daniel McLaughlin wrote:[color=blue]
    > I have the following code that matches just fine.
    >
    > var re;
    > re = /23-Apr-2004/gi;
    > alert(re.test(d ocument.all['thebody'].innerHTML));
    >
    > However, when I attempt it with a variable containing the same characters it
    > thows the error
    > "Object doesn't support this property or method" on the test line below.
    >
    > var months = new
    > Array('Jan','Fe b','Mar','Apr', 'May','Jun','Ju l','Aug','Sep', 'Oct','Nov','De c
    > ');
    > var now = new Date();
    > var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
    > now.getYear();
    > today = "/"+today+"/gi";[/color]

    the reg ex would be:
    eval("/"+today+"/gi");
    Personally, I would not name the variable "today"
    [color=blue]
    > var re;
    > re = today;
    > alert(re.test(d ocument.all['thebody'].innerHTML));[/color]

    <body onload="alert(r e.test(document .all['thebody'].innerHTML));">
    Mick[color=blue]
    >
    > Can anyone tell me why this isn't working and perhaps a way around it?
    >
    >[/color]

    Comment

    • Mark Szlazak

      #3
      Re: regexp not matching variable

      For dynamic or variable regular expressions you need to use the new
      RegExp(pattern, modifiers) constructor not literals. The 'pattern' is
      your 'today' string which leaves out those forward slashes, they're for
      delimiting regular expression literals. This example should give you the
      general idea.

      var today = 'april' + '-' + '22' + '-' + '2004';
      var rx = new RegExp(today, 'gi');








      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: regexp not matching variable

        Mick White <mwhite13@roche ster.rr.com> writes:
        [color=blue][color=green]
        >> today = "/"+today+"/gi";[/color][/color]

        This just makes today a string, not a regexp ...
        [color=blue]
        > the reg ex would be:
        > eval("/"+today+"/gi");[/color]

        I would recommend against using eval. Too many things can go wrong.
        Use the RegExp constructor instead:
        today = RegExp(today,"g i");

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • bruce

          #5
          Re: regexp not matching variable

          "Daniel McLaughlin" <javascript@den ial.co.uk.nospa m> wrote in message news:<108267738 1.340493@news01 .eclipse.net.uk >...[color=blue]
          > I have the following code that matches just fine.
          >
          > var re;
          > re = /23-Apr-2004/gi;
          > alert(re.test(d ocument.all['thebody'].innerHTML));
          >
          > However, when I attempt it with a variable containing the same characters it
          > thows the error
          > "Object doesn't support this property or method" on the test line below.
          >
          > var months = new
          > Array('Jan','Fe b','Mar','Apr', 'May','Jun','Ju l','Aug','Sep', 'Oct','Nov','De c
          > ');
          > var now = new Date();
          > var today = now.getDate() + "-" + months[now.getMonth()] + "-" +
          > now.getYear();
          > today = "/"+today+"/gi";
          > var re;
          > re = today;
          > alert(re.test(d ocument.all['thebody'].innerHTML));
          >
          > Can anyone tell me why this isn't working and perhaps a way around it?[/color]

          -----------------------------

          You cannot define your regular expression dynamically as such:
          reg="/" + "abc" + "/"

          This just doesn't work. At least not for me.
          It is NOT a string.

          You would define it as just
          reg=/abc/

          If you want to dynamically construct a regular expression, use
          regex = new RegExp (pattern)

          In this last example, you can define your pattern dynamically.

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: regexp not matching variable

            Lasse Reichstein Nielsen wrote:
            [color=blue]
            > Use the RegExp constructor instead:[/color]
            ^^^^^^^^^^^[color=blue]
            > today = RegExp(today,"g i");[/color]

            Although RegExp(x, y) is specified in ECMAScript 3 as a factory,

            today = new RegExp(today, "gi");

            is saner.


            PointedEars

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: regexp not matching variable

              bruce wrote:
              [color=blue]
              > You cannot define your regular expression dynamically as such:
              > reg="/" + "abc" + "/"
              >
              > This just doesn't work. At least not for me.[/color]

              For nobody. Atigs. (And that is good so.)
              [color=blue]
              > It is NOT a string.[/color]

              That's why concatenation of strings does not create a RegExp object.


              PointedEars

              Comment

              Working...