本文介绍了替换或删除第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
发布评论