【问题标题】:Replace last occurrence word in javascript [duplicate]替换javascript中最后出现的单词[重复]
【发布时间】:2014-06-01 22:25:32
【问题描述】:

我在替换 JS 中的最后一个单词时遇到问题,我仍在寻找解决方案,但我无法得到它。

我有这个代码:

var string = $(element).html(); // "abc def abc xyz"
var word   = "abc";
var newWord = "test";

var newV   = string.replace(new RegExp(word,'m'), newWord);

我想替换此字符串中的最后一个单词“abc”,但现在我只能替换字符串中的全部或第一次出现。我怎样才能做到这一点?也许不是好办法?

【问题讨论】:

标签: javascript regex replace


【解决方案1】:

您可以使用 replace 方法在不使用全局标志的情况下仅替换目标字符串的第一次出现的事实,并尝试以下操作:

"abc def abc xyz abc jkl".split(' ').reverse().join(' ').replace('abc', 'test').split(' ').reverse().join(' ')

【讨论】:

    【解决方案2】:

    这是一个想法......

    这是一个区分大小写的字符串搜索版本

    var str = 'abc def abc xyz';
    var word = 'abc';
    var newWord = 'test';
    
    // find the index of last time word was used
    // please note lastIndexOf() is case sensitive
    var n = str.lastIndexOf(word);
    
    // slice the string in 2, one from the start to the lastIndexOf
    // and then replace the word in the rest
    str = str.slice(0, n) + str.slice(n).replace(word, newWord);
    // result abc def test xyz
    

    如果您想要不区分大小写的版本,则必须更改代码。让我知道,我可以为你改变它。 (PS。我正在做,所以我会尽快发布)

    更新:这是一个不区分大小写的字符串搜索版本

    var str = 'abc def AbC xyz';
    var word = 'abc';
    var newWord = 'test';
    
    // find the index of last time word was used
    var n = str.toLowerCase().lastIndexOf(word.toLowerCase());
    
    // slice the string in 2, one from the start to the lastIndexOf
    // and then replace the word in the rest
    var pat = new RegExp(word, 'i')
    str = str.slice(0, n) + str.slice(n).replace(pat, newWord);
    // result abc def test xyz
    

    注意上面的代码寻找一个字符串。不是整个单词(即 RegEx 中的单词边界)。如果字符串必须是一个完整的单词,则必须对其进行修改。

    更新 2:这是一个不区分大小写的正则表达式全词匹配版本

    var str = 'abc def AbC abcde xyz';
    var word = 'abc';
    var newWord = 'test';
    
    var pat = new RegExp('(\\b' + word + '\\b)(?!.*\\b\\1\\b)', 'i');
    str = str.replace(pat, newWord);
    // result abc def test abcde xyz
    

    祝你好运 :)

    【讨论】:

      【解决方案3】:
      // create array
      var words = $(element).html().split(" ");
      
      // find last word and replace it
      words[words.lastIndexOf("abc")] = newWord 
      
      // put it back together
      words = words.join(" ");
      

      【讨论】:

      • 我会怎么做。所有这些正则表达式的答案对于简单的事情来说都太复杂了!
      • 只更改最后一个单词,而不是最后一个“abc”
      • 现在很好,但应该是words = words.join(" ")
      【解决方案4】:

      你想要两个:

      • 匹配abc
      • 检查字符串中是否没有其他abc

      所以你可以使用:

      abc(?!.*abc)
      

      (?!...) 是一个否定的前瞻,如果前瞻中的内容匹配,它将导致整个正则表达式匹配失败。

      还要小心,因为这会匹配 abcdaire 中的 abc:如果您只想将 abc 作为单独的单词,则需要添加单词边界 \b

      \babc\b(?!.*\babc\b)
      

      【讨论】:

      • 出于某种原因string.replace(/\babc\b(?!.*\babc\b)/, '') 将最后一个 abc 替换为空格,你知道为什么吗? jsfiddle.net/5q9K8/11
      • 真的不应该……这不只是因为你删除了一个单词,现在左右两边的空格就相邻了?
      • 哈哈是的,正是因为如此,我很愚蠢。那么,好的答案
      【解决方案5】:

      试试

      var string = $(element).html(); // "abc def abc xyz"
      var word   = "abc";
      var newWord = "test";
      
      var newV   = string.replace(new RegExp(word+'$'), newWord);
      

      【讨论】:

        【解决方案6】:

        我对 JavaScript 不太熟悉,但您可以根据自己的需要调整一下:

        (\b\w+\b)(.*)(\1) 替换为\1\2+'your_key_word'

        See the demo 看看我的意思。

        【讨论】:

        • 请不要将代码跨度用于“JavaScript”之类的内容。
        【解决方案7】:

        您可以使用前瞻来获取句子中的最后一个单词:

        var string = "abc def abc xyz";
        var repl = string.replace(/\babc\b(?!.*?\babc\b)/, "test");
        //=> "abc def test xyz"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-08
          相关资源
          最近更新 更多