【问题标题】:Javascript: using String.replace() to replace/remove HTML anchorsJavascript:使用 String.replace() 替换/删除 HTML 锚点
【发布时间】:2013-08-16 13:30:22
【问题描述】:

如果有办法用另一个简单的字符串替换一个简单的字符串,我正在尝试完成一项非常简单的任务。

我有一个页面的 HTML 源代码作为字符串。它包含几个内部锚点,例如

所有的锚点都存储在一个数组中(['about','contact'],...),我需要删除每个出现的字符串,如

href="#whatever"

(每次都有不同的东西)所以结果是

我要做的简单搜索和替换是遍历我的数组并替换每次出现的

'href="'+anchorname+'"'

带有一个空字符串。但是在多次尝试使用 string.replace() 之后,我仍然没有找到实现这一点的方法。


换句话说(也发布在 cmets 中): 提出我的问题的一个更简单的方法是:

假设我的字符串包含以下三个字符串

如何使用 Javascript 将它们(但不是任何具有相同模式的标签)替换为

【问题讨论】:

标签: javascript string search replace


【解决方案1】:
All of the anchors are stored in an array (['about','contact'],...), and I need to remove every occurance of a string like href="#whatever" (where whatever is each time something different) so that the result is

为此,您可以执行以下操作:

var tets_array = $("a[href^='#']"); // select all a tags having href attr starting with #

然后取出数组并修改为你想要的。

【讨论】:

  • 听起来他对整个 HTML 内容都有一个大字符串值,他想操纵它。我猜他没有 jQuery 或任何其他可访问的库来提供$(...) 访问功能。
  • 你不觉得他的问题很模糊。很难理解
  • 最初的问题很模糊,因为这是我第一次在不知道如何使我的代码正确显示的情况下提出问题。我现在修好了。问题是关于使用 javascript String.replace() 函数来替换不是通用模式,而是替换包含 " 和 # 等字符的特定字符串。
【解决方案2】:

我不知道我是否理解这个问题,但你可以试试这个:

var index = myArray.indexOf('whatever');

myArray[index] = "whatever2"

【讨论】:

    【解决方案3】:

    如果我理解正确,那么您可以使用正则表达式替换所有这些。

    var tagString = '<a href="#contact"> <a href="#what"> <a href="#more">';
    
    // Find every occurrence which starts with '#' and ends with '"'
    var regEx = new RegExp('#.*?"', 'g'); 
    
    // Replace those occurrences with '"' to remove them
    tagString = tagString.replace(regEx, '"');
    

    编辑:

    要替换数组中的特定标签,您可以执行以下操作:

    var tags = ['<a href="#a">', '<a href="#b">', '<a href="#c">'];
    var tagsToReplace = ['a', 'c'];
    
    for (var i = 0, len = tags.length; i < len; i++) {
        var matches = tags[i].match(/#.*"/);
        if (matches === null) {
            continue;
        }
    
        var anchor = matches[0].substr(1).replace('"', ''); // Get only the anchor
        if (tagsToReplace.indexOf(anchor) !== -1) {
            tags[i] = tags[i].replace('#' + anchor, 'replaced');
        }
    }
    

    【讨论】:

    • 谢谢。不,我只想替换特定锚的列表(存储在数组中),而不是所有匹配模式的子字符串。
    【解决方案4】:

    一个具体的陈述如下:

    var mystring = '<a href="#thistag"> <a href="#thattag">';
    var repstring = mystring;
    var myarray = ['thistag', 'theothertag'];
    
    for (var i = 0; i < myarray.length; i++) {
      var reptag = myarray[i];
      repstring = repstring.replace('a href="#' + reptag + '"', 'a');
    }
    

    然后repstring 包含最后的字符串。请注意交替的单引号和双引号字符,它们确保双引号作为 HTML 文本的一部分位于它们通常的位置。显然您可以更改reptag(即myarray 的内容)以包含# 字符,此时您将更改mystring.replace(...) 行以匹配。

    【讨论】:

      【解决方案5】:

      感谢所有建议。我尝试了它们的各种变体,发现有些在 Firefox 中有效,但在 chrome 中无效,这导致我进入了这个线程

      Why doesn't the javascript replace global flag work in Chrome or IE, and how to I work around it?

      这让我找到了解决方案:

      for (var i=0; i < myTags.length ; i++)
      {
              var find = 'href="#' + myTags[i] + '"';
              var regex = new RegExp(find, 'gi');
              MyWholeString = MyWholeString.replace(regex, '');
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-09
        • 2019-01-06
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        相关资源
        最近更新 更多