替换或删除第n个\ n

编程入门 行业动态 更新时间:2024-10-07 09:23:24
本文介绍了替换或删除第n个\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的字符串:

var ok = "\n\n33333333333\n\n\n";

如何用''替换'\ n'的第四次出现? 或者,如何删除"\ n"的第四次出现?

How to replace the 4th occurence of '\n' with ''? Or, how to remove the 4th occurence of '\n'?

推荐答案

  • 找到n-1次出现和其他字符.
  • 捕获子匹配项.
  • 找到下一个事件.
  • 将整个匹配项替换为捕获的子匹配项,然后 替换字符/字符串

    • find n-1 occurences and other characters.
    • capture the submatch.
    • find the next occurence.
    • replace the entire match with the captured submatch, and the replacement character/string

      "AA33333333333AAA".replace(/((?:[^A]*A){3}[^A]*)A/,"$1k")

      (具有\n和""的A和k安装,因此您可以更清楚地看到结果)

      (with A and k insteead of \n and "" so you can more clearly see the results)

更多推荐

替换或删除第n个\ n

本文发布于:2023-11-30 15:20:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或删除

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!