skip to Main Content

refering to Replace words with array map
the answer is good, but I would like to add global replace to it.

const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];

let string = "its beginner in the sounds leave";

arrString.forEach((mapString) => {
  const stringArr = mapString.split("=");
  string = string.replace(stringArr[1], stringArr[0]);
});

console.log("string", string);

I tried

string.replace(/stringArr[1]/p, stringArr[0]);

Tried it on a string having the same word 3 times.
not working, only the first is replaced.

2

Answers


  1. To perform a global replace using a regular expression in JavaScript, you can use the g flag.

    just modify your code

    const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];
    
    let string = "its beginner in the sounds leave";
    
    arrString.forEach((mapString) => {
      const stringArr = mapString.split("=");
      const regex = new RegExp(stringArr[1], "g");
      string = string.replace(regex, stringArr[0]);
    });
    
    console.log("string", string);
    
    Login or Signup to reply.
  2. Just use String::replaceAll(), no need in a regular expression here. Also it’s confusing that you have your replacements reversed. Semantically I guess would be better beginner = (means replace beginner with) beginning.

    const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];
    
    let string = "its beginner in the sounds leave. beginner again...";
    
    arrString.forEach(mapString => string = string.replaceAll(...mapString.split("=").reverse()));
    
    console.log("string", string);

    If you want regexp anyway:

    const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];
    
    let string = "its beginner in the sounds leave. beginner again...";
    
    const replacements = Object.fromEntries(arrString.map(item => item.split('=').reverse()));
    
    string = string.replace(new RegExp(Object.keys(replacements).join('|'), 'g'), m => replacements[m]);
    
    console.log("string", string);

    The regexp version seems a bit faster:

    enter image description here

    <script benchmark data-count="1">
    
        const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];
    
        let string = "its beginner in the sounds leave. beginner again...".repeat(1000000);
    
        // @benchmark String::replaceAll()
        {
            let str = string;
            arrString.forEach(mapString => str = str.replaceAll(...mapString.split("=").reverse()));
            str;
        }
    
        // @benchmark regexp
        {
            const str = string;
            const replacements = Object.fromEntries(arrString.map(item => item.split('=').reverse()));
            str.replace(new RegExp(Object.keys(replacements).join('|'), 'g'), m => replacements[m]);
        }
    
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search