Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:56:37+00:00 2026-05-24T18:56:37+00:00

I have a small web app where I need to perform text replacement in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:56:37+00:00Added an answer on May 24, 2026 at 6:56 pm

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small web app that generate PDF files as a report. I'm trying
I am a broke college student. I have built a small web app in
So I have been developing a small ASP.NET web app in C# for my
I have developed a small web app. This app allows users to upload images.
I need to build a small web app that will ultimately need to be
We have following problem. Developers frequently need to make small changes to our web
I am using Django to create a small web app, however I do not
I have a small web app running on AppEngine and have all my URL
I have written a small web app to collect some data and store it
So, I'm working on a small web app and I have it to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.