javascript使用for循环从多个字符串中删除字符

javascript removing characters from multiple strings using a for loop

本文关键字:字符串 删除 字符 使用 for 循环 javascript      更新时间:2024-02-29

我正在使用文件读取器读取日志文件,然后想使用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,'');
}

您也可以对其进行修剪以删除前导和跟随的空白

arrayOfLines[x] = arrayOfLines[x].trim();