-1

How do I replace all instances where I have #sometext# in order of the array provided?

The text is a string and my goal is to search for all instances where I have some text surrounded by the "#" character. Then replace those instances with values in the array in the order of the array

Example:

const text = "You are the #RANKED# highest ranked user (out of #TOTALUSER# people)"

const myArray = [10, 50];

After searching and replacing the text should be:

"You are the 10 highest ranked user (out of 50 people)"
James Chege
  • 23
  • 1
  • 7

1 Answers1

1
text.replace(/#([^#]+)#/g, _ => myArray.shift());
  • Thanks for answering. The text is a string and my goal is to search for all instances where I have some text surrounded by # character. Then replace those instances with values in the array in the order of the array. – James Chege Aug 19 '20 at 08:50
  • Oh, sorry I didn't read your question carefully. Thanks for the other comment, I got the idea. You may study [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). – Jonnel VeXuZ Dorotan Aug 19 '20 at 09:02