简体   繁体   中英

Part of the string is missing when trying to replace

I have a string that looks like this:

[TITLE|prefix=X|suffix=a] [STORENAME|prefix=b] [DYNAMIC|limit=10|seperator=-|random=1|reverse=1|prefix=c]

I would like to replace the values of all prefix attributes with hello . So the goal is that the string looks like this:

[TITLE|prefix=hello|suffix=a] [STORENAME|prefix=hello] [DYNAMIC|limit=10|seperator=-|random=1|reverse=1|prefix=hello]

This is what I have tried:

 const obj = {}; obj.prefix = "[TITLE|prefix=a|suffix=x] [STORENAME|prefix=b] [DYNAMIC|limit=10|seperator=-|random=1|reverse=1|prefix=c]"; function replace(search, replace) { const regex = new RegExp(`(?<=\\\\[${search}\\\\|[^\\\\]]*${replace}=)[^|\\\\]]+`); obj.prefix = obj.prefix.replace(regex, 'hello'); } replace('TITLE', 'prefix'); replace('STORENAME', 'prefix'); replace('DYNAMIC', 'prefix'); console.log(obj.prefix);

As you see it works fine!

I have used almost the same code for my project but it fails. You can check my project on JSFiddle . Just type anything on an input field and check the console. You will see that the value of first prefix will be changed but 2 further prefix attributes are missing.

So this is what I get:

[TITLE|prefix=anything|suffix=a] [STORENAME] [DYNAMIC|limit=10|seperator=-|random=1|reverse=1]

And this is what I should get:

[TITLE|prefix=anything|suffix=a] [STORENAME|prefix=another thing] [DYNAMIC|limit=10|seperator=-|random=1|reverse=1|prefix=one more thing]

What is the reason that those attributes are missing?

Update

If I am not mistaking, my main problem is the if-statement:

if (mtPr.query[mtPr.settings.activeLang].includes(replace)) {

With this if-statement, I would like to check if either TITLE has the attribute prefix or STORENAME has the attribute prefix or DYNAMIC has the attribute prefix . But this is a bad workaround since the value of replace is always prefix (see line numbers 241, 245 and 251). And since we already have prefix in the WHOLE string, it means that we're caught in that if-statement every single time. So a possible solution could be, checking if the parameter replace is included AND does it belong to the parameter search .

Try this

function replace(search, replace) {
  const regex = new RegExp(`(${search}[^\\[\\]]*\\|prefix\\=)[^\\|\\[\\]]+`); 
  obj.prefix = obj.prefix.replace(regex, '$1hello');
}

As I have described in my question, the problem was really the if-statement. This how I could resolve it:

  const searchMatchesReplace = new RegExp(`(?<=${search}.+${replace}=)[^\\]|]+`);
  if (searchMatchesReplace.test(mtPr.query[mtPr.settings.activeLang])) {
    const regex = new RegExp(`(?<=\\[${search}\\|[^\\]]*${replace}=)[^|\\]]+`, 'g');
    result = mtPr.query[mtPr.settings.activeLang].replace(regex, value);
  }
  // Replace parameters if they do not exist
  else {
    const regex = new RegExp(`(\\[${search}(?:\\|[^\\][]*)?)]`, 'gi');
    result = mtPr.query[mtPr.settings.activeLang].replace(regex, `$1|${replace}=${value}]`);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM