1

I have a small web app where I need to perform text replacement in a string. However, the normal string.replace won't work. I am modifying portions of the text and then I need to put them back in the original string. For example, if I have:

var text = 'TEXT 000.99 001.00 TEXT'

and I need to add 0.01 to each of these numbers, I have an array of results:

var new_vals = [001.00, 001.01]

Then, when I try to replace 000.99 with 001.00, it will work and give me the following string:

text = 'TEXT 001.00 001.00 TEXT'

now I face the problem. The next replacement should be 001.00 <- 001.01, but doing so would result in:

text = 'TEXT 001.01 001.00 TEXT'

My question is, do you know of any JS library that provides a replace function that can perform multiple search/replace at the same time and on a copy of the string?

0

3 Answers 3

3

Use str.replace with a function:

var text = 'TEXT 000.99 001.00 TEXT';
var new_vals = ["001.00", "001.01"];
var index = 0;
//Returns a new result string
var result = text.replace(/\b([\d.]+)\b/g, function(substr, num, offset, str){
  //See if the number is actually a number
  var num = parseFloat(num);
  //If not, return original
  if(isNaN(num))
    return substr;
  //Return the number from new_vals and increment the index
  else
    return new_vals[index++];
});
//result == "TEXT 001.00 001.01 TEXT"

Or, you could do the addition in the function:

var text = 'TEXT 000.99 001.00 TEXT';
//Returns a new result string
var result = text.replace(/\b([\d.]+)\b/g, function(substr, num, offset, str){
  //See if the number is actually a number
  var num = parseFloat(num);
  //If not, return original
  if(isNaN(num))
    return substr;
  //Return num+0.01 with 2 decimal places
  else
    return (num+0.01).toFixed(2);
});
//result == "TEXT 1.00 1.01 TEXT"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I just reached the same conclusion while reading the ECMA script drafts. It says about string.replace: "once a replacement is performed, the new replacement text is not subject to further replacements." Kind of like what I wanted, a replace that works on a copy.
0

Try using the global replace regexp instead of a simple string. The simple string will replace only the first matching instance.

'TEXT 001.00 001.00 TEXT'.replace(/001.00/g, '001.01');

2 Comments

I did. That's when I discovered the problem. The first replacement of 000.99 with 001.00 goes well. The problem is that when I try to replace 001.00 with 001.01, it replaces the first 001.00 (the one we got after replacing 000.99) as well. I need a function that performs multiple replacements on a string while working on a copy.
Are you sure that you included the trailing 'g' in the regexp. That should force subsequent replacements.
0

You could try split(' ') the values into an array. Then modifying the specific elements using a loop. Followed by join(' ') to join the text back into a string.

1 Comment

The problem is that while in the question all the text has just one space b/w words, this might not be the case with the real text. And keeping the number of spaces intact is important for this app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.