如果使用正则表达式在URL中排除字符串?

时间:2016-03-05 13:17:44

标签: regex

我正在替换许多不同的字符串,但只希望它们在普通文本中替换,并且当它们作为文档中的链接出现时不会被重写。查找字符串的正则表达式非常简单:/word|anotherword|athirdword/gi但这意味着如果有一个包含anotherword的链接,它会被找到然后被替换,从而破坏链接。

我认为我只需要我的正则表达式中的一部分说“但只是忽略以http或https开头的任何内容”,但不知道如何编写它。

非常感谢!

修改。这是我正在用javascript做的事情

if (node.nodeType === 3) {
        var text = node.nodeValue;
        var replacedText = text.replace(/word|anotherword|athirdword/gi, 'replaced text');

        if (replacedText !== text) {
            element.replaceChild(document.createTextNode(replacedText), node);
        }
    }

结果会在页面的任何位置替换这三个字符串,这很棒。除了将http://www.foo.com/the-whole-world更改为http://www.foo.com/the-whole-replaced text,这显然会破坏链接。

2 个答案:

答案 0 :(得分:0)

我会尝试消极的看法。

负面的背后观察因风味而异,因此它不会在不同的风格中起作用。

对于JavaScript,您可以尝试以下操作:

str.replace(/(http:[\/\.-a-z0-9]+)?(word|anotherword|athirdword)/gi, function($0, $1){
 return $1 ? $0 : '';
});

aggregate

答案 1 :(得分:0)

您可以先拆分字符串,然后执行条件替换:

function condReplace(str) {
  var sentences = [];
  var res = str.split(/(https?:\/\/[^\s]+)(?:\s+|$)/i);
  res.forEach(function(entry) {
    if (entry) {
      if (entry.match(/^http?:\/\//i)) {
        sentences.push(entry);
      } else {
        sentences.push(entry.replace(/word|anotherword|athirdword/g, "REPLACED"))
      }
    }
  });
  document.write(sentences.join(" "));
}

var str = "http://sometext.com/word.doc and This is a word normal text anotherword containing a anotherword another link http://www.foo.com/the-whole-word. This is a single word.";
condReplace(str);