[英]Javascript: Insert text at specified line
I am kind of new to javascript, but I was looking to see if it was possible to add a line of text to a file at a specified position? 我是javascript的新手,但我想看看是否可以在指定位置的文件中添加一行文字? ie every file needs to have the same text inserted at line no. 即每个文件需要在第一行插入相同的文本。 3 3
Someone suggested "split by line feed, add data, then rejoin", but i have no clue how to do that. 有人建议“按换行,添加数据,然后重新加入”,但我不知道如何做到这一点。 Just looking for somewhere to get started or pointed in the right diretion. 只是寻找一个开始或指向正确方向的地方。
Split by line feed: 按换行符拆分:
data = data.split(/\r?\n/);
Add data: 添加数据:
data = data.splice(2, 0, new_data);
Rejoin: 重新加入:
data = data.join("\r\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.