【问题标题】:javascript removing characters from multiple strings using a for loopjavascript使用for循环从多个字符串中删除字符
【发布时间】:2015-02-22 15:18:28
【问题描述】:

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

【问题讨论】:

    标签: javascript arrays string for-loop


    【解决方案1】:

    字符串是不可变的,你必须把它写回去

    for(x=0;x<arrayOfLines.length;x++) {
        arrayOfLines[x] = arrayOfLines[x].replace(/    /g,'');
    }
    

    您也可以只修剪它以删除前导和后续空格

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2020-04-13
      • 1970-01-01
      相关资源
      最近更新 更多