我正在使用文件阅读器读取日志文件,然后想使用 javascript 进行一些文本操作,以便在我的程序中进一步使用读取的数据。到目前为止,我设法按行拆分我的输入,但现在我想格式化数组中的特定字符串,但没有任何反应。这是因为没有全局声明数组吗?基本上我想做一个 for 循环来检查我的数组中的所有字符串并删除出现在我的一些字符串开头的“”(四个空格)。这是我的代码
$("#draftlog").change(function() {
var logFile = $('#draftlog').get(0).files[0];
//gets first file from draftlog
var reader = new FileReader;
reader.readAsText(logFile);
reader.onload = function(e) {
var rawLog = reader.result;
//reads first file from draftlog as text
var re=/\r\n|\n\r|\n|\r/g;
arrayOfLines = rawLog.replace(re,"\n").split("\n");
//splits the text into an array of strings for every new line
for(x=0;x<arrayOfLines.length;x++) {
arrayOfLines[x].replace(/ /g,'');
}
console.log(arrayOfLines);
};
});
我的输入通常是这样的:
Event #: 7952945
Time: 5.2.2015 17:14:54
Players:
TheDoktorJot
Arlekin
Jokulgoblin
Felo
Petrolit
Greyjoy
--> Susti
themuse1975
n0sfea
------ FRF ------
Pack 1 pick 1:
Abzan Runemark
Rakshasa's Disdain
Reach of Shadows
Grim Contest
Aven Skirmisher
Lotus Path Djinn
Formless Nurturing
Tasigur's Cruelty
Temur Battle Rage
Return to the Earth
--> Temur Sabertooth
Fascination
Jeskai Barricade
Arcbond
Rugged Highlands
Pack 1 pick 2:
Sandblast
Sultai Runemark
Jeskai Sage
Hooded Assassin
Pressure Point
Gore Swine
Whisperer of the Wilds
Mardu Runemark
Ambush Krotiq
Write into Being
Qarsi High Priest
Hewed Stone Retainers
Wardscale Dragon
--> Mastery of the Unseen
最佳答案
字符串是不可变的,你必须把它写回去
for(x=0;x<arrayOfLines.length;x++) {
arrayOfLines[x] = arrayOfLines[x].replace(/ /g,'');
}
您也可以只 trim 它以删除开头和后面的空格
arrayOfLines[x] = arrayOfLines[x].trim();
关于javascript使用for循环从多个字符串中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28659495/