RegExp can't find a period

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

    RegExp can't find a period

    Is there any reason why RegExp wouldn't be able to find a period?

    str = '256.89';
    a = new RegExp('(\.)');
    b = /(\.)/;
    alert(str.repla ce(a, 'x$1x'));
    alert(str.repla ce(b, 'x$1x'));

    The alert for a says
    x2x56.89

    However the alert for b says
    256x.x89

    b is the result I want, because it means that the period was found. a
    is just finding a single character, as if it's ignoring the escape in
    front of the period.

    I've tried this in IE and Mozilla.
  • Steve van Dongen

    #2
    Re: RegExp can't find a period

    On 15 Jan 2004 01:01:23 -0800, trialofmiles@ya hoo.com (trialofmiles)
    wrote:
    [color=blue]
    >Is there any reason why RegExp wouldn't be able to find a period?
    >
    >str = '256.89';
    >a = new RegExp('(\.)');[/color]

    Here's a hint:
    When used in a STRING, the backslash is the escape character.
    [color=blue]
    >b = /(\.)/;
    >alert(str.repl ace(a, 'x$1x'));
    >alert(str.repl ace(b, 'x$1x'));
    >
    >The alert for a says
    >x2x56.89
    >
    >However the alert for b says
    >256x.x89
    >
    >b is the result I want, because it means that the period was found. a
    >is just finding a single character, as if it's ignoring the escape in
    >front of the period.
    >
    >I've tried this in IE and Mozilla.[/color]

    Regards,
    Steve

    Comment

    • Harag

      #3
      Re: RegExp can't find a period

      On Thu, 15 Jan 2004 11:14:05 GMT, Steve van Dongen
      <stevevd@hotmai l.com> wrote:
      [color=blue]
      >On 15 Jan 2004 01:01:23 -0800, trialofmiles@ya hoo.com (trialofmiles)
      >wrote:
      >[color=green]
      >>Is there any reason why RegExp wouldn't be able to find a period?
      >>
      >>str = '256.89';
      >>a = new RegExp('(\.)');[/color]
      >
      >Here's a hint:
      >When used in a STRING, the backslash is the escape character.[/color]

      Yeah, what you actually need to do is:

      a = new RegExp('(\\.)') ;

      when its a string.

      hth
      Al


      Comment

      • trialofmiles

        #4
        Re: RegExp can't find a period

        Steve van Dongen <stevevd@hotmai l.com> wrote in message news:<hctc005p6 i00mjoqqp0tc5sd 7gjfhcje6n@4ax. com>...[color=blue]
        > Here's a hint:
        > When used in a STRING, the backslash is the escape character.[/color]

        Sometimes the solution is so simple. Thank you very much.

        What's strange is at one point I had written it \\. and things weren't
        working the way I expected. But that was when I was trying it as part
        of a larger regular expression. Another piece of the expression must
        have been wrong. Things are working now.

        Comment

        Working...