www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Better way to find and replace a number

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

    resolved [RESOLVED] Better way to find and replace a number

    When I find a record such as this in a textarea:
    Code:
    |MPC|Controller:tempo|Style:Linear Sweep|TimeRes:Quarter|SweepRes:1|Pt1:0,20|Pt2:20,28|Pt3:7,40|Pt4:33,56|Pos:8|Wide:Y
    I would like to multiply the 2nd number after a Ptn: field by tfactor (already calculated) and replace it. Thus far I have
    Code:
    	if ((lines[i].search("|MPC|Controller:tempo") == -1))
    	  continue;
    	result = lines[i].match(/Pt1:\d+,(\d+)/);  
    	if (result != null) {
    	  old_tempo = result[1];
    	  new_tempo = Math.round(old_tempo * tfactor);
    	  lines[i] = lines[i].replace(old_tempo,new_tempo);	}
        else
          continue;	
    	result = lines[i].match(/Pt2:\d+,(\d+)/);  
    	if (result != null) {
    	  old_tempo = result[1];
    	  new_tempo = Math.round(old_tempo * tfactor);
    	  lines[i] = lines[i].replace(old_tempo,new_tempo); }
        else
          continue;	//...... similar code for 3 & 4 follows
    There will be from 1 to 4 Pts, if one is missing there will not be a next one.

    My problem is I replace the first occurrence of any number I find and for certain factors (2 in this case) it could change an already converted number and leave other fields in the record alone. I could split the line on commas to change the first number I find but can't be certain if there are no other commas elsewhere on the line.

    TIA

    P.S The page is tchange.htm.
    Last edited by wbport; 02-12-2011 at 10:58 AM. Reason: Insert link

  2. #2
    Join Date
    Dec 2010
    Posts
    207
    Code:
    <script type='text/javascript'>
    
    str = 
    "|MPC|Controller:tempo|Style:Linear Sweep|TimeRes:Quarter|SweepRes:1|Pt1:0,20|Pt2:20,28|Pt3:7,40|Pt4:33,56|Pos:8|Wide:Y";
    
    function change( str, tfactor )
    {
     var rxp, idx = 1, data, startFrom, foundAt;
     
     while( data = ( rxp = new RegExp( "(Pt)(" + (idx++) + ")(:\\d+,)(\\d+)",'g') ).exec( str ) )
     {
      foundAt = data.index;
      startFrom = rxp.lastIndex;  
      str = str.substring( 0, foundAt ) + data[ 1 ] + data[ 2 ] + data[ 3 ] + ( Number( data[ 4 ] ) * tfactor ) + str.substring( startFrom );
     }
     
     return str;   
    }
    
    document.write( str + '<br>===========================<br>');
    
    document.write( change(str, 2) ); /* Multiply by 2 */
    
    </script>


    Q: I found this code...
    A: Then find its author.

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    resolved

    Many thanks! Just what I needed. I knew it needed something involving regular expressions and making a substitution at only specific places, but i didn't know where to begin except for an awkward workaround.

    I credit you on the final version (link on 1st message).

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