最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

TypescriptJavascript: Replace All String Occurrences with Random Number - Stack Overflow

programmeradmin0浏览0评论

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}}}}

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}}}}
Share Improve this question edited Apr 19, 2017 at 19:37 ctilley79 asked Apr 19, 2017 at 19:31 ctilley79ctilley79 2,1953 gold badges32 silver badges66 bronze badges 6
  • What is result before you call replaceAll()? – DontVoteMeDown 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. – 9000 Commented Apr 19, 2017 at 19:35
  • You can generate N unique numbers, one for each RegExp match, see stackoverflow./a/40063045 – guest271314 Commented Apr 19, 2017 at 19:37
  • @DontVoteMeDown I edited my answer to show value of result before – ctilley79 Commented Apr 19, 2017 at 19:39
  • 1 @ctilley79 Ok, i've added your input string into my code. It seems to work well. – DontVoteMeDown Commented Apr 19, 2017 at 19:42
 |  Show 1 more ment

2 Answers 2

Reset to default 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.

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");
    }

发布评论

评论列表(0)

  1. 暂无评论