Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using replace()--is there a 'start position' option? 1

Status
Not open for further replies.

jsteph

Technical User
Oct 24, 2002
2,562
US
Hi,
I want to use the standard replace() function, without using a RegExp in it, and I want to be able to either:

a. Replace ALL occurences of a string,
...or
b. Allow me to loop and pass a 'start position' parameter to mimick the same functionality of option a.

Is there an undocumented argument for the normal replace() or some other (native) function that will do that--again, without the Regexp?
Thanks,
--J
 
afaik,

a. no
b. no

what are you trying to do?


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
You could try a version of this idea:

xxold = "X"; // replace value
xxnew = "y"; // with this value

var hold_in = whereever_your_original_value_is_from ; // hold value

while (hold_in.indexOf(xxold)>-1) {
foundhere = hold_in.indexOf(xxold);
hold_in = "" + (hold_in.substring(0, foundhere) + xxnew +
hold_in.substring((foundhere + xxold.length), hold_in.length));
}

This will replace all occurrences of the xxold string.
You could modify the while loop to control when to start and stop.
 
Thanks, I'll steer away from looking for the 'start' argument. But maybe RegExp is what I need--I read a bit about that and I think that, contrary to what I had at first said--they may fit the bill exactly. Here is what I'm trying to do:

I have a large block of text that has several repeating patterns which I want to replace part of the pattern. For example, here is some sample text:

"Todays sales for plant 45 were 52,000 units. Last week's sales for plant 32 were 42,000 units. There were 62,000 units in other areas"

Ok, I want to--without going through all sorts of string parsing and cutting blocks of text with substring()--I want to increment only numbers 52,000 and 42,000 by 1000 each--but NOT the number 62,000, because that number is not preceded by the a pattern of 'plant nn were'.

So in the old DOS days, I could bulk rename a bulk group of files by using wildcards, and the rename function would preserve the original value of each wildcard but replace the rest with whatever you want to replace. This is what I want--I would like to replace the pattern 'plant nn were nnnnn' with a replacement string of 'plant nn were (nnnnn + 1000)', where 'nn' remains whatever plant number it orignally was.

The first set will always be 2 digits, and the second will always be 5 digits with a comma, so there won't be complications of randomly sized nn patterns.

I would like to do this with a one-line native function like replace. I already have code that does excactly what I need, but it's many lines of code and is using substring-this plus substring-that, and it does not seem efficient. I read a little about regular expressions and it seems they should have the capability to do exactly what I need. Is this possible?
--Jim
 
woohoo! got to use a newer overloaded version of String.replace(), which has the signature String.replace( pattern, func );

where func is a function which is passed the match to pattern, and returns the replacement value. in the case of submatches, they will be passed as additional args to func.

voila:

Code:
var s = "Todays sales for plant 45 were 52,000 units.  "
	+ "Last week's sales for plant 32 were 42,000 units.  "
	+ "There were 62,000 units in other areas.";

var pattern = /plant (\d+) were (\d{1,3}(,\d{3})*)/gi;

function doIt() {
	alert( s.replace(pattern, increment) );
}
function increment(match, plant, total) {
	total = commas(parseInt(total.replace(/,/g, ""), 10) + 1000);
	return "plant " + plant + " were " + total;
}
function commas(n) {
	//  remove existing commas
	n = n.toString().replace(/,/g,"");

	//  rebuild string with commas
	var sNew = "";
	var sTemp = n;
	var c = 0;

	for (idx = sTemp.length - 1; idx >= 0; idx--) {
		sNew = sTemp.charAt(idx) + sNew;
		c++;
		if (c == 3 && idx > 0) {
			sNew = "," + sNew;
			c = 0;
		}
	}
	return sNew;
}

the only ugly part is reformatting the new numbers with commas. this part too could be replaced with a short regex, but would require forward & backward lookups, which i don't think javascript regex supports yet.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
jeff,
That was perfect!! That was the exact silver bullet I needed--easily 40 lines of code distilled into 2 plus the function call. If I could give multiple stars, I would--that is fantastic!
--J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top