JavaScript? Putting a variable in the midst of a Regular Expression.

Ask a Question related to Adobe Acrobat Macintosh, Design and Development.

  1. #1

    Default JavaScript? Putting a variable in the midst of a Regular Expression.

    I was reading a great article by Kas Thomas, "Save Yourself Some Typing with 'With'" at PlanetPDF. I really don't care much for the term guru, I think it is way overused, but if anyone deserved it, it would be him. Really great informative JavaScript info and he is funny too.

    He had this script chunk below and it was very close to something I am doing. Although, how I was going about it was much less elegant so I wanted to adapt his example to what I was trying to accomplish.

    var subtotal = 0.0;
    var fld;

    with (this) { // default object is 'this'

    for (var k = numFields; k; k--)
    {

    fld = getField(getNthFieldName(k-1));
    if (fld.name.match(/amt/i))
    subtotal += fld.value;

    }
    }

    Although the article is about using the 'with' statement to conserve and compact your code what caught my eye was this very elegant loop for iterating through a number of fields, a very common need.

    But what has stopped me is that his example... (fld.name.match(/amt/i)) is a fixed match for a known word, in this case, any occurence of 'amt'. I need to have a variable in the middle of the same RegEx because my search is arbitrary, I do not know what the word value will be, but I have not seen an example of this anywhere.

    Does anyone know how this bit of syntax gymnastics might be accomplished?
    Garrett_Cobarr@adobeforums.com Guest

  2. Similar Questions and Discussions

    1. Regular Expression
      Hi, I am writing a script that parses an html file (which has been retrieved as a scalar by LWP::UserAgent). The script looks for everything in...
    2. Regular expression help
      Hi, I'm pretty new to regular expressions. Before, I used to write long-winded and buggy segments of code with PHPs string functions to extract...
    3. Regular Expression - Need Help
      Hi, I have the following: <cfset input =" Origin: cohnok-530a (190.068.59.250) Type: ...
    4. javascript regular expression error
      It works here but it's not fool proof. You didn't say why it isn't working for you. It could be that your putting in odd characters in the field or...
    5. [PHP] Regular Expression
      Thanks Wendell. This is exactly what I was looking for. -----Original Message----- From: Wendell Brown Sent: Tuesday, July 08, 2003 4:47 AM...
  3. #2

    Default Re: JavaScript? Putting a variable in the midst of a Regular Expression.

    Try this for the if statement:

    if (util.printx("<*",fld.name).match(reqstrvar))

    where reqstrvar is the variable containing the required string.
    Graham_O_Connor@adobeforums.com Guest

  4. #3

    Default Re: JavaScript? Putting a variable in the midst of a Regular Expression.

    Graham, thanks! I just stumbled on your answer, the forum is acting up and not sending out subscriptions.

    Here is a link to George's response on Planet PDF...
    <http://forum.planetpdf.com/webboard/wbpx.dll/~planetpdfforum>
    Garrett_Cobarr@adobeforums.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139