0

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
  • This works, but only for the first time it appears. Is there any way to make it work multiple times? Commented Oct 24, 2022 at 20:20
  • Without a while loop with a huge statement, I mean Commented Oct 24, 2022 at 21:55

2 Answers 2

1

Yes, you need to utilize the .replace function and make use of RegEx. In your case this would look like this:

str.replaceAll(/(§k.*?)bar(.*?§r)/g, "$1newBar$2");

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Right sorry forgot the global flag updating now
Okay so it seems that you cant embed other strings into regex, i updated the answer but it seems its matching the most outer §k... §r if theres any pattern inbetween them you're gonna have to find it and im gonna try to find a fix inbetween.
I added a not operator however the pattern I'm matching is merely an example so i suggest you try and match the actual string, it might prove easier and regex is fairly easy to learn. I guess it might work now tho
It does not seem to work
Yeah but does then it matches the shortest one, does that really fit with the replaceAll method? Revised the code accordingly hope it works now
-1

Solution:

let input = "§kfoo bar 123 bar baz§rfoo bar 123 bar§kfoo bar 123 barbar baz§r";

let result = replace(input, "§k", "§r", "bar", "BAR");
console.log(input);
console.log(result);

function replace(input, beginStr, endStr, match, replaceWith) {
    let beginIndex = input.indexOf(beginStr);
    let endIndex = input.indexOf(endStr, beginIndex);

    while (beginIndex > -1 && endIndex > -1) {
        let matchIndex = input.indexOf(match, beginIndex);

        while (matchIndex > beginIndex && matchIndex < endIndex) {
            input = input.substring(0, matchIndex) + replaceWith + input.substring(matchIndex + match.length);

            matchIndex = input.indexOf(match, beginIndex);
        }


        beginIndex = input.indexOf(beginStr, endIndex);
        endIndex = input.indexOf(endStr, beginIndex);
    }
    return input;
}

Should output:

§kfoo bar 123 bar baz§rfoo bar 123 bar§kfoo bar 123 barbar baz§r
§kfoo BAR 123 BAR baz§rfoo bar 123 bar§kfoo BAR 123 BARBAR baz§r

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.