Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
これは私の文字列です:
var ok = "\n\n33333333333\n\n\n";
'\n' の 4 番目の発生を '' に置き換える方法は? または、「\n」の 4 番目の出現を削除する方法は?
少なくとも私にとっては、読みやすい解決策は次のとおりです。
var i = 0; ok = ok.replace(/\n/g, function () { return ++i == 4 ? "" : "\n"; });
ただし、パフォーマンスに関する限り、勝てない可能性があります。