3

I have the following function that replaces a string occurence with a random number. What I have currently replaces the string with the same random number. I need a function that replaces each instance a unique number. Here's what I have.

Attempt to Clarify:

I want to find all occurrences of '},{ and replace it with },"random":{ where random is a unique integer for each occurrence.

result = this.replaceAll(result, '},{', `},"${this.getRandomInt(1, 2000)}":{`);

private getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

private replaceAll(str, find, replace) {
  return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
}

private escapeRegExp(str) {
  return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

Currently the result is something to the effect of

},"1340":{"expense_category_id":"63","amount":3},"1340":{"expense_category_id":"62","amount":3}}}}

1340 should not have duplicated

Edit: The value of result before replace all is:

},{"expense_category_id":"63","amount":3},{"expense_category_id":"62","amount":3}}}}
6
  • What is result before you call replaceAll()? Commented Apr 19, 2017 at 19:35
  • Do not replace. Split the input by }, {, then re-join it by }, number:{, replacing the number every time in a loop. Commented Apr 19, 2017 at 19:35
  • You can generate N unique numbers, one for each RegExp match, see stackoverflow.com/a/40063045 Commented Apr 19, 2017 at 19:37
  • @DontVoteMeDown I edited my answer to show value of result before Commented Apr 19, 2017 at 19:39
  • 1
    @ctilley79 Ok, i've added your input string into my code. It seems to work well. Commented Apr 19, 2017 at 19:42

2 Answers 2

7

Pass a function to the replace() second parameter like this:

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
}

function escapeRegExp(str) {
  return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

var input = '},{"expense_category_id":"63","amount":3},{"expense_category_id":"62","amount":3}}}}',
    result = replaceAll(input, '},{', (x => '},"' + getRandomInt(1, 2000) + '":{'));

console.log(result);

I'm not sure why, but it seems that it uses the same string generated for the first iteration for subsequent iterations. With a function, you force it to run everytime.

Sign up to request clarification or add additional context in comments.

Comments

1

Your replace call is wrong. You're passing in a string, you should be passing in a function that creates the number, not the result of the number itself.

    let result = "{A},{B},{C}";
    result = replaceAll(result, '},{', () => { return `},"${this.getRandomInt(1, 2000)}":{` });
    console.log(result);
    
    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    function replaceAll(str, find, replace) {
      return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
    }
    
    function escapeRegExp(str) {
      return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }

1 Comment

Cool. I see what you mean now. I'm going to fiddle around with this.

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.