Skip to content
Advertisement

How can I remove occurrences with random hash?

I have a controlled <input /> in my React application where the user would type.

I want to keep track on it and replace the occurrences of $.text and $.lang to random hashes/number.

from

JavaScript

I want all occurrences of $.text and $.lang to have a random number(or anything unique):

to

JavaScript

What I have done so far

JavaScript

The issue is it replaces the but onChange RESETS the input state and it only changes the last occurrence of it.

Advertisement

Answer

So im doing it on click. So lets say user type something and then clicking on Replace Template button will replace the $.text and $.lang by some random number. You can insert your specific hash or number if needed.

JavaScript

inside this arrow function I’m running the while loop till we find the template to replace, and replace occurrence by random number.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement