【问题标题】:Replace some words in string, but keep another替换字符串中的一些单词,但保留另一个
【发布时间】:2018-05-19 12:00:14
【问题描述】:

我正在尝试替换字符串中的一些单词,所以我们有一个编码字符串:

%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D

里面有几句话:

  • %25dontreplace%25
  • %25dontreplace%25
  • %25replace1%25
  • %25replace2%25
  • %25replace3%25

我需要用%replaceX%替换所有%25replaceX%25,但不要碰所有%25dontreplace%25(每个可能有不同的数量)

我正在尝试将其拆分为数组并执行一些 ifelse 语句,但它似乎不起作用,我认为这不是最优雅的:

var x = "%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D";
    console.log(x);
    var y = x.split(/(%25)/);
    console.log(y);
    var n = [];
    for (var i = 0; i <= y.length; i++) {
    if (y[i].indexOf('%25') >= 0) {
            if (y[i + 1].indexOf('request.puid') >= 0) {
            n.push('%'+y[i]+'%');
            console.log(n);
        }
    } else {
          if (y[i - 1] === undefined && y[i + 1] === '%25') {
             n.push(y[i]);
             console.log(n);
          } else if (y[i - 1] === '%25' && y[i + 1] === undefined) {
             n.push(y[i]);
             console.log(n);
          } else if (y[i - 1] === '%25' && y[i + 1] === '%25') {
             n.push('%25'+y[i]+'%25');
             console.log(n);
          }
        }   
    }

有没有优雅的方法来解决这个问题?

【问题讨论】:

  • 你试过研究正则表达式吗?
  • .replace(/%25/,"%")

标签: javascript arrays string iteration


【解决方案1】:

您可以使用捕获组将.replaceRegExp 结合使用。

我们将使用的RegExpRegExp(/%25replace([0-9]*)%25/, 'g'),它查找与%25replaceX%25 匹配的字符串,g 查找所有匹配项。 () 捕获了数字,因此我们可以在替换文本中使用它。

类似这样的:

var str = "%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D";

var newStr = str.replace(new RegExp(/%25replace([0-9]*)%25/, 'g'), "%replace$1%");

document.write(newStr)

【讨论】:

  • 布雷特,非常感谢!有用。学习 RegExp 的最佳做法是什么?
猜你喜欢
  • 2020-01-14
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
相关资源
最近更新 更多