I have a string that would look something like this:
var str = '§kfoo bar 123§rfoo bar 123§kfoo bar 123§r';
// ^ ^
and I want to replace "bar" only when it appears between §k and §r (in this example, the 1st and 3rd ones).
Just replacing them in that string is not an option, that is just an example. The string is a user input, which means the amount of substrings will vary.
Is there any way to do that?
2
Answers
Solution:
Should output:
Yes, you need to utilize the
.replace
function and make use of RegEx. In your case this would look like this:I believe this pattern should work, but I’m not 100% percent sure so let me know if it does and research RegEx pattern matching to further troubleshoot it.