替换或删除第n个\ n [英] Replace or remove nth occurrence of \n

查看:95
本文介绍了替换或删除第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""Ak安装,因此您可以更清楚地看到结果)

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

      这篇关于替换或删除第n个\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆