www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Runaway script with regular expression.

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374

    resolved [RESOLVED] Runaway script with regular expression.

    I am trying to globally change a line like:
    |Dynamic|Style:ff|Pos:-8 to
    |Dynamic|Style:ff|Opts:Velocity=127,Volume=108|Pos:-8 wherever it appears in a textarea and I have eight Styles and Volumes to loop through.
    Some sample data:
    Code:
    |Dynamic|Style:fff|Pos:-8
    |Note|Dur:4th|Pos:1
    |Dynamic|Style:ff|Pos:-8
    |Note|Dur:4th|Pos:1
    |Dynamic|Style:f|Pos:-8
    |Note|Dur:4th|Pos:1
    |Dynamic|Style:mf|Pos:-8
    |Note|Dur:4th|Pos:1
    |Bar
    |Dynamic|Style:mp|Pos:-8
    |Note|Dur:4th|Pos:1
    |Dynamic|Style:mf|Pos:-8
    and what I have so far:
    Code:
    <head>
    <title>Custom Dynamics</title>
    <script language="JavaScript">
    var default_volumes = [ 127, 108, 92, 75, 60, 45, 30, 10 ]
    function reload() {
    for (var i = 0; i < default_volumes.length; i++)
      document.nwcform.elements[i].value = default_volumes[i];
    }
    function calculate() {
    var dyn_table = [ "fff", "ff", "f", "mf", "mp", "p", "pp", "ppp" ]
    var dyn_vol1 = new Array();
    for (var i = 0; i < 8; i++)
      dyn_vol1[i] = parseInt(document.nwcform.elements[i].value);
    var str = document.nwcform.OutputField.value;
    
    for (i = 0; i < 8; i++) {
     var rxp, idx = 1, data="", startFrom=0, foundAt=0;
     
     while( data = ( rxp = new RegExp( "(\|Dynamic\|Style:)(" + dyn_table[i] + ")(\|Pos)",'g') ).exec( str ) )
     {
      foundAt = data.index;
      startFrom = rxp.lastIndex;  
      str = str.substring( 0, foundAt ) + data[ 1 ] + data[ 2 ] + "|Opts:Velocity=127,Volume=" + dyn_vol1[i] + data[ 3 ] +  + str.substring( startFrom );
    //      Stuff before found expr     "Dynamic|Style:" ff-pp       insert literal             dyn volume    "Pos"           Stuff after Pos   
     }
     
    }
    document.nwcform.OutputField.value = str;
    document.nwcform.OutputField.select();
    }
    </script>
    </head>
    <body onLoad="reload()">
    <form name="nwcform">
      <table>
        <tr>
          <td><B><I>fff </I></B><input size="4"></td>
          <td><B><I>ff </I></B><input size="4"></td>
          <td><B><I>f </I></B><input size="4"></td>
          <td><B><I>mf </I></B><input size="4"></td>
          <td><B><I>mp </I></B><input size="4"></td>
          <td><B><I>p </I></B><input size="4"></td>
          <td><B><I>pp </I></B><input size="4"></td>
          <td><B><I>ppp </I></B><input size="4"></td>
    	  <td><input type="button" name="smbutton" value="Submit" onclick="calculate()"></td>
    	  <td><input type="button" name="rstbutton" value="Reset" onclick="reload()"></td>
    	</tr><tr>
        <td colspan="9" width="1044" height="200"><textarea rows="30" cols="120" name="OutputField" width="2044"></textarea></td></tr>
    </table>
    </form>
    Thanks in advance.
    Last edited by wbport; 02-24-2011 at 04:20 PM. Reason: Remove comments to reduce size

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Never mind. I split it into lines to work on it poco a poco, building a from and to string for a replace.

  3. #3
    Join Date
    Dec 2010
    Posts
    207
    Code:
    while( data = ( rxp = new RegExp( "(\|Dynamic\|Style:)(" + dyn_table[i] + ")(\|Pos)",'g') ).exec( str ) )
    You should not constantly re-generate a regex that uses the g flag. This has the effect of resetting the lastIndex pointer, thereby causing an infinite loop.

    Edit: Looking closer at this and recognising my own code, I now realise that I told you this previously.
    Last edited by clueful; 02-25-2011 at 07:32 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles