简体   繁体   中英

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? ie every file needs to have the same text inserted at line no. 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM