Replace nth occurrence of string

前端 未结 3 1235
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 01:16

For example:

\'abcjkjokabckjk\'.replace(\'/(abc)/g\',...)

If I want to replace a specify position \'abc\', what I can do?

Like this

相关标签:
3条回答
  • 2021-01-14 01:43

    Use RegExp constructor to pass variables inside regex.

    var s = 'abcjkjokabckjk'
    search = 'abc'
    var n = 2
    alert(s.replace(RegExp("^(?:.*?abc){" + n + "}"), function(x){return x.replace(RegExp(search + "$"), "HHHH")}))

    0 讨论(0)
  • 2021-01-14 01:48

    Use Avinash's approach with RegExp constructor

    const replace_nth = function (s, f, r, n) {
        // From the given string s, replace f with r of nth occurrence
        return s.replace(RegExp("^(?:.*?" + f + "){" + n + "}"), x => x.replace(RegExp(f + "$"), r));
    };
    

    Here's an example outout.

    $node
    Welcome to Node.js v13.1.0.
    Type ".help" for more information.
    >
    >  const replace_nth = function (s, f, r, n) {
    ...    // From the given string s, replace f with r of nth occurrence
    ...    return s.replace(RegExp("^(?:.*?" + f + "){" + n + "}"), x => x.replace(RegExp(f + "$"), r));
    ...};
    
    > replace_nth('hello world', 'l', 'L', 1)
    'heLlo world'
    
    0 讨论(0)
  • 2021-01-14 01:54

    This can be done without RegEx.

    String methods String#indexOf, String#lastIndexOf can be used with String#substring

    var string = 'abcde|abcde|abcde|abcde',
      needle = 'abc',
      firstIndex = string.indexOf(needle),
      lastIndex = string.lastIndexOf(needle);
    
    // ----------------------------------------------------------------
    // Remove first occurence
    var first = string.substring(0, firstIndex) + '***' + string.substring(firstIndex + needle.length);
    document.getElementById('first').innerHTML = first;
    
    // ----------------------------------------------------------------
    // Remove last occurence
    var last = string.substring(0, lastIndex) + '***' + string.substring(lastIndex + needle.length);
    document.getElementById('last').innerHTML = last;
    
    // ----------------------------------------------------------------
    // Remove nth occurence
    // For Demo: Remove 2nd occurence
    var counter = 2, // zero-based index
      nThIndex = 0;
    
    if (counter > 0) {
      while (counter--) {
        // Get the index of the next occurence
        nThIndex = string.indexOf(needle, nThIndex + needle.length);
      }
      
      // Here `nThIndex` will be the index of the nth occurence
    }
    
    var second = string.substring(0, nThIndex) + '***' + string.substring(nThIndex + needle.length);
    document.getElementById('second').innerHTML = second;
    table tr td:nth-child(2) {
      color: green;
    }
    td {
      padding: 15px;
    }
    <table border="1px">
      <tr>
        <td>After replacing <strong>first</strong> occurence:</td>
        <td id="first"></td>
      </tr>
      <tr>
        <td>After replacing <strong>last</strong> occurence:</td>
        <td id="last"></td>
      </tr>
      <tr>
        <td>After replacing <strong>2nd<sup>zero-based index</sup></strong> occurence:</td>
        <td id="second"></td>
      </tr>
    </table>

    0 讨论(0)
提交回复
热议问题