繁体   English   中英

替换第 n 次出现的字符串

[英]Replace nth occurrence of string

例如:

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

如果我想替换指定位置“abc”,我能做什么?

像这样:在此处输入图像描述

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

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

使用 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'

这可以在没有 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>

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM