Variable inside RegEx

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Er Galv?o Abbott

    Variable inside RegEx

    Greetings.

    I have a function that does some pattern matching with JS's RegEx and
    I'm trying to use a variable inside of it. Nothing that I've done
    worked, so please help me.

    Here is the func:

    function validateField(f ormField,limit)
    {
    if (formField.matc h(/^\d{1,11}\,\d{2 }$/))
    {
    return true;
    }
    }

    What I need is to replace the "11" in the RegEx with the var limit.

    How can I do this???

    TIA,
  • Michael Winter

    #2
    Re: Variable inside RegEx

    On 16 Feb 2004 09:19:00 -0800, Er Galv?o Abbott <galvao@galvao. eti.br>
    wrote:
    [color=blue]
    > Greetings.
    >
    > I have a function that does some pattern matching with JS's RegEx and
    > I'm trying to use a variable inside of it. Nothing that I've done
    > worked, so please help me.
    >
    > Here is the func:
    >
    > function validateField(f ormField,limit)
    > {
    > if (formField.matc h(/^\d{1,11}\,\d{2 }$/))
    > {
    > return true;
    > }
    > }
    >
    > What I need is to replace the "11" in the RegEx with the var limit.
    >
    > How can I do this???[/color]

    As you've no doubt discovered, literals cannot have variables in them.
    However, the RegExp() constructor can:

    function validateField( formField, limit ) {
    var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );

    return formField.match ( re );
    }

    If you need to use flags, add them as a string as the second parameter.
    Global, for example:

    ... = new RegExp( 'pattern', 'g' );

    Hope that helps,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Variable inside RegEx

      galvao@galvao.e ti.br (Er Galv?o Abbott) writes:
      [color=blue]
      > if (formField.matc h(/^\d{1,11}\,\d{2 }$/))[/color]

      (no need to escape the comma)
      [color=blue]
      > What I need is to replace the "11" in the RegEx with the var limit.[/color]

      If the regular expression isn't constant, you can't use the literal
      notation, and must construct the regular expression using the RegExp
      function instead.

      RegExp("^\\d{1, " + limit + "},\\d{2}$" )

      Notice that the argument to RegExp is a string literal, and as such
      treats backslashes special. To include a backslash in the generated
      regular expression, you must write two in the string literal.

      /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

      • Richard Cornford

        #4
        Re: Variable inside RegEx

        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opr3g5hdvj 5vklcq@news-text.blueyonder .co.uk...
        <snip>[color=blue]
        > function validateField( formField, limit ) {
        > var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );[/color]
        <snip>

        But is necessary to escape the escape characters in the string literals
        provided for the RegExp constructor, something like (untested):-

        var re = new RegExp( '^\\d{1,' + limit + '}\\,\\d{2}$' );

        Richard.


        Comment

        • Michael Winter

          #5
          Re: Variable inside RegEx

          On Mon, 16 Feb 2004 18:39:59 -0000, Richard Cornford
          <Richard@litote s.demon.co.uk> wrote:
          [color=blue]
          > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          > news:opr3g5hdvj 5vklcq@news-text.blueyonder .co.uk...
          > <snip>[color=green]
          >> function validateField( formField, limit ) {
          >> var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );[/color]
          > <snip>
          >
          > But is necessary to escape the escape characters in the string literals
          > provided for the RegExp constructor, something like (untested):-[/color]

          [snipped example]

          *Slaps forehead*

          Quite right.

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          Working...