how would i randomly replace a string in a text using javascript or jquery?
given: <p>foo bar foo baz foo</p>
I want to just randomly replace 1 occurrence of ‘foo’ with ‘buz’, so possible results would be:
<p>buz bar foo baz foo</p>
<p>foo bar buz baz foo</p>
<p>foo bar foo baz buz</p>
Explanation:
First of all we have to know the range to the
random. We can get it matching the string and then get thelength.Then you have to match it again and increase a counter which will be used to know which match is the currently one.
If the current match is equal to the random number we have to replace the text.
demo