www.webdeveloper.com
Results 1 to 3 of 3

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

Threaded View

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

    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 11:58 AM. Reason: Insert link

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