regex 替换第n次出现的字符串

xmq68pz9  于 2022-12-24  发布在  其他
关注(0)|答案(4)|浏览(221)

例如:

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

如果我想替换一个指定的位置'abc',我该怎么做?
像这样:

vltsax25

vltsax251#

使用RegExp构造函数在正则表达式中传递变量。

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

d6kp6zgx2#

使用RegExp模块创建如下函数

const replace_nth = function (s, f, r, n) {
    // From the given string s, find f, replace as r only on n’th occurrence
    return s.replace(RegExp("^(?:.*?" + f + "){" + n + "}"), x => x.replace(RegExp(f + "$"), r));
};

这里有一个例子。

$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'
ctehm74n

ctehm74n3#

这可以在没有RegEx的情况下完成。
字符串方法String#indexOfString#lastIndexOf可以与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>
iqxoj9l9

iqxoj9l94#

function replaceNth(
  string: string,
  from: string,
  to: string,
  n: number
): string {
  const pattern = RegExp(escapeRe(from));
  let nth = 0;
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const result = string.replace(pattern, function (match, i, original) {
    nth++;
    return nth === n ? to : match;
  });
  console.log(`${string} => ${result}`);
  return result;
}

相关问题