0

I am using "[TITLE] [STORENAME] [DYNAMIC]".replace(regex, `$1|${replace}=${value}]`); multiple times but everytime before I use it, I change the variable replace. See the full code:

obj = {} || obj;
const replace = 'prefix';
let search = 'TITLE';
const value = 'MY VALUE';
const regex = new RegExp(`(\\[${search}(?:\\|[^\\][]*)?)]`, 'gi');

obj.str = "[TITLE] [STORENAME] [DYNAMIC]".replace(regex, `$1|${replace}=${value}]`);

search = 'STORENAME';
obj.str = "[TITLE] [STORENAME] [DYNAMIC]".replace(regex, `$1|${replace}=${value}]`);

search = 'DYNAMIC';
obj.str = "[TITLE] [STORENAME] [DYNAMIC]".replace(regex, `$1|${replace}=${value}]`);

console.log(obj.str);

So as you see I get the result [TITLE|prefix=MY VALUE] [STORENAME] [DYNAMIC] but the goal is to get this result: [TITLE|prefix=MY VALUE] [STORENAME|prefix=MY VALUE] [DYNAMIC|prefix=MY VALUE].

Probably it is because I use the same string for replace() all over again but I have also tried it this way:

obj = {} || obj;
obj.str = '[TITLE] [STORENAME] [DYNAMIC]';

const replace = 'prefix';
let search = 'TITLE';
const value = 'MY VALUE';

const regex = new RegExp(`(\\[${search}(?:\\|[^\\][]*)?)]`, 'gi');

obj.str = obj.str.replace(regex, `$1|${replace}=${value}]`);

search = 'STORENAME';
obj.str = obj.str.replace(regex, `$1|${replace}=${value}]`);

search = 'DYNAMIC';
obj.str = obj.str.replace(regex, `$1|${replace}=${value}]`);

console.log(obj.str);

But the output is still not correct.

1
  • 1
    Once const regex = new RegExp((\[${search}(?:\\|[^\][]*)?)], 'gi'); runs it is set in stone... It will not update when you change the variable Commented Oct 27, 2020 at 13:13

2 Answers 2

2

Check out this code:

obj = {} || obj;
obj.str = '[TITLE] [STORENAME] [DYNAMIC]';

const replace = 'prefix';
const value = 'MY VALUE';

obj.str.split(" ").forEach(function(val){
  let search = val.replace("[", "").replace("]", "");
  let regex = new RegExp(`(\\[${search}(?:\\|[^\\][]*)?)]`, 'gi');
  obj.str = obj.str.replace(regex, `$1|${replace}=${value}]`);
})


console.log(obj.str);

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

Comments

1

You assume the reg exp updates automatically. It does not, you would need to make a reg exp for each one.

Use a look up object with a generic reg exp.

var replacements = {
 FOO: "Hello",
 BAR: "World",
 BAZ: "Funky Pants"
};

var reExp = /\[([^\]]+)\]/g;

// function replaceIt(str, replacements) {
//  return str.replace(reExp, function (match) {
//    return replacements[match.substring(1, match.length-1)] || match;
//  });
// }

function replaceIt(str, replacements) {
  return str.replace(reExp, function (match) {
    var key = match.substring(1, match.length-1);
    var text = replacements[key];
    if (!text) return match
    return `[${key}|prefix=${text}]`;
  });
}

console.log(replaceIt("[FOO] [BAR] [BAZ]", replacements));
console.log(replaceIt("[FOO] [BAR] [CHEESE]", replacements));

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.