Regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Québec

    Regex

    I am trying to make this onClick work:
    onClick="Format Str(('E',('F'(' G'('H')))))">

    ===============
    var strTable = "\
    <table border=1 width='100%'>\
    <tr>\
    <td width='50%'>@1</td>\
    <td>@2</td>\
    </tr>\
    <tr>\
    <td>@3</td>\
    <td>@4</td>\
    </tr>\
    </table>"

    function FormatStr(str) {
    alert(arguments[0]+arguments[1]+arguments[2]+arguments[3]+arguments[4])
    for ( i=1; i < arguments.lengt h; i++ )
    str = str.replace(new RegExp("@"+i), arguments[i]);
    return str;
    }

    function doIt() {
    document.body.i nsertAdjacentHT ML("beforeEnd" , FormatStr(strTa ble, "A",
    "B", "C", "D"));
    }

    <FORM >
    <INPUT TYPE="button" value="Table" onClick="doIt() ">
    <INPUT TYPE="button" value="FormatSt r" onClick="Format Str(('E',('F'('
    G'('H')))))">
    </FORM>


  • Thomas 'PointedEars' Lahn

    #2
    Re: Regex

    Québec wrote:
    [color=blue]
    > I am trying to make this onClick work:
    > onClick="Format Str(('E',('F'(' G'('H')))))">[/color]

    In Mozilla/5.0:
    | Error: string is not a function
    | Source File: javascript: alert(('E',('F' (' G'('H')))))
    | Line: 1

    In IE 6.0 SP-1 (translated):
    | Line: 1
    | Column: 1
    | Error: Function expected
    | Code: 0
    | URL: [not of interest]

    You are using the `(' operator without a keyword or an identifier
    left-hand side or an expression inside. What did you want the above
    argument(s) to display?
    [color=blue]
    > ===============
    > var strTable = "\
    > <table border=1 width='100%'>\
    > <tr>\
    > <td width='50%'>@1</td>\
    > <td>@2</td>\
    > </tr>\
    > <tr>\
    > <td>@3</td>\
    > <td>@4</td>\
    > </tr>\
    > </table>"[/color]

    Although supported in web browsers, you should not rely on `\' to continue
    the string literal in the next line in all UAs as the JavaScript reference
    specifies nothing of the kind. Use the concatenation operator `+' instead.


    HTH

    PointedEars

    Comment

    • Québec

      #3
      Re: Regex

      doIt prints a table with the letters A B C D in each cell.

      onClick="Format Str('E', 'F',' G',('H'))))" is suppose to replace A B C D
      with E F G H



      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Regex

        Québec wrote:
        [color=blue]
        > doIt prints a table with the letters A B C D in each cell.
        >
        > onClick="Format Str('E', 'F',' G',('H'))))" is suppose to replace
        > A B C D with E F G H[/color]

        `(...)' outside of a string literal is nothing but a paranthesed
        expression and thus here ('H') is the same as 'H'. And there are
        more parantheses closed (4) than opened (2) here. I guess you are
        looking for

        onClick="Format Str('E', 'F',' G', '(\'H\')')"

        Besides,

        function FormatStr(str) {
        alert(arguments[0]+arguments[1]+arguments[2]+arguments[3]+arguments[4])
        for ( i=1; i < arguments.lengt h; i++ )
        str = str.replace(new RegExp("@"+i), arguments[i]);
        return str;
        }

        will not work as supposed since "E" is assigned to `str' (as it is
        the first argument or ...arguments[0]) and there is no `@x' in it to
        replace.

        function FormatStr() {
        // i as local variable and arguments is zero-based
        for (var i = 0; i < arguments.lengt h; i++)
        strTable =
        strTable.replac e(new RegExp("@" + (i + 1)), arguments[i]);
        return strTable;
        }

        will do the trick one time. If you need `strTable' unchanged, assign
        its value to a local variable and refer to that variable for replace.

        See

        and

        for details.


        HTH

        PointedEars

        Comment

        • Québec

          #5
          Re: Regex

          I think, I have not been specific enough. The idea is to change the letters
          in the already made table with the second function.
          ----------------
          var strTable = "";
          strTable += "<table border=1 width='100%'>"
          strTable += "<tr>"
          strTable += "<td width='50%'>@1</td>"
          strTable += "<td>@2</td>"
          strTable += "</tr>"
          strTable += "<tr>"
          strTable += "<td>@3</td>"
          strTable += "<td>@4</td>"
          strTable += "</tr>"
          strTable += "</table>"

          function FormatStr(str) {
          for ( i=1; i < arguments.lengt h; i++ )
          str = str.replace(new RegExp("@"+i), arguments[i]);
          return str;
          }

          function doIt() {
          document.body.i nsertAdjacentHT ML("beforeEnd" , FormatStr(strTa ble, "A",
          "B", "C", "D"));
          }


          //--</script

          Substitutes @1 with arg1, @2 with arg2 and so on at the passed
          string.

          <p><FORM >
          <INPUT TYPE="button" value="doIt" onClick="doIt() ">
          <INPUT TYPE="button" value="FormatSt r2" onClick="Format Str2('E', 'F',' G',
          'H')">
          </FORM>


          Comment

          Working...